利用ModalViewController切换View
来源:互联网 发布:车铣复合加工编程书 编辑:程序博客网 时间:2024/05/16 18:13
当程序中含有多个 view,需要在之间切换的时候,可以使用 UINavigationController,或者是 ModalViewController。UINabigationController 是通过向导条来切换多个 view。而如果 view 的数量比较少,且显示领域为全屏的时候,用 ModalViewController 就比较合适(比如需要用户输入信息的view,结束后自动回复到之前的view)。今天我们就看看 ModalViewController 的创建方法。
ModalViewController 并不像 UINavigationController 是一个专门的类,使用 UIViewController 的 presentModalViewController 方法指定之后就是 ModalViewController 了。
这里使用上两回做成的 CustomViewController(由UIViewController继承)来实现 ModalViewController 的实例。
首先,准备 ModalViewController 退出时的函数。调用 UIViewController 的 dismissModalViewController:Animated: 方法就可以了,如下所示:
接下来,生成另一个 CustomViewController 的实例,用来表示 ModalViewController,并将其对应的 view 设置成红色。然后传递给 presentModalViewController: Animated: 显示 ModalViewController 的 view。
编译执行以后,首先启动的是红色背景的 ModalViewController view、按下按钮后恢复到蓝色背景的通常 view 上。
也可以在显示 ModalViewController view 之前设置 UIViewContrller 的 modalTransitionStyle 属性,使其以动画形式显示。
以上的实现只是单一地实现了 ModalViewController view 的功能,除了程序开始提醒用户一些信息外什么也做不了。另外由于是放入了 applicationDidFinishLaunching 中的原因,也不能反复的显示。另外,在 ModalViewController view 上设置的内容也不能反映到原来的 view 上。
接下来我们将实现这些功能。
首先,从 ModalViewController view 退出的时候,需要通知原先的 view。这里使用 iPhone/Cocoa 应用程序中经常使用的Delegate 设计模式(也是推荐使用的)。
实际上,系统所提供的图像选择控制类 UIImagePickerController或者是参照地址簿时的ABPeoplePickerNavigationController 类,都用到了 Delegate 模式。
基于上一讲的中的例子,这里我们追加为3个按钮,分别是绿色,灰色和取消。
程序启动的时候依然是先显示 ModalViewController view,按下任何一个按钮,将关闭该view。按下“绿色”按钮,设置背景为绿色,按下“灰色”按钮时,设置背景为灰色。“取消”的时候什么也不做。
委托处理用下面的函数实现,当参数 inColor 为 nil 的时候代表取消。
委托代理的实例用 id 变量表示。
设置该变量的函数如下。
另外如上面 viewDidLoad 所示,按钮的 tag 分别为0、1、2。按钮按下时调用的函数中由不同的 tag 来发送不同的 UIColor实例到 colorSelectDelegate 上。
这是不使用 UIButton* 而是用 UIView* ,是因为 tag 属性被定义在 UIView 类中,不需要必须转换为 UIButton 类。另外这样一来,该函数在 UIButton 以外的情况下也能被使用。如果想检查 id 是什么类性的可以使用 isKindOfClass: 方法。
接收到具体的参数 inColor 更换背景色,并关闭 ModalViewController view。
另外,在调用 presentModalViewController 之前(显示 ModalViewController view 之前),需要设定委托的实例。
编译一下,程序启动后显示红色背景的 ModalViewController view,点击绿色按钮后,原先的view的背景变为绿色,点击灰色,显示灰色的背景,而点击取消,那么将显示原先蓝色的背景。
这样的形式,就是将按钮的动作委托给原先view的 Controller 来处理了。根据送来的 UIColor 来设置不同的背景色。
转自:http://www.yifeiyang.net/iphone-developer-advanced-7-use-modalviewcontroller-switch-view/
- 利用ModalViewController切换View
- 利用ModalViewController切换View
- iOS: 利用ModalViewController切换View
- iPhone开发进阶--- 利用ModalViewController切换View
- iPhone开发之利用ModalViewController切换View
- iPhone开发进阶--利用ModalViewController切换View
- iPhone开发之利用ModalViewController切换View
- iPhone开发之利用ModalViewController切换View
- 用ModalViewController切换View
- iPhone开发进阶(7)— 利用ModalViewController切换View
- iPhone开发进阶(7)--- 利用ModalViewController切换View
- iPhone开发进阶————利用ModalViewController切换View
- iPhone开发之ModalViewController视图切换
- 如何解散modalViewController的modalViewController
- iPhone View切换动画
- iphone 实现View切换
- SegmentControl点击切换View
- iOS: 切换view
- vector中循环与删除
- ZOJ Problem Set - 3490 String Successor
- asp.net .ashx,cs文件使用server.mappath解决方法
- linux异步IO浅析
- 开发软件下载地址
- 利用ModalViewController切换View
- android解析XML文件
- 【线段树+离散化+离线方法】杭电 hdu 3333 Turing Tree
- ZOJ Problem Set - 3607 Lazier Salesgirl
- 单链表实现冒泡排序
- 建立一个带头结点的的单向链表并输出到out53.dat和屏幕上。各节点的值为对应的下表。链表的节点数及输出地文件名作为参数传入
- 基于S3C2440——SD/MMC
- 黑马程序——String类(黑马我来了之九)
- 在ContextMenu中添加/删除ListView的Item