iOS开发中ViewController的页面跳转和弹出模态
2016-06-22 14:04
701 查看
ViewController 页面跳转
从一个Controller跳转到另一个Controller时,一般有以下2种:
1、利用UINavigationController,调用pushViewController,进行跳转;这种采用压栈和出栈的方式,进行Controller的管理。调用popViewControllerAnimated方法可以返回。
复制代码代码如下:
PickImageViewController *ickImageViewController = [[PickImageViewController alloc] init];
[self.navigationController pushViewController: ickImageViewController animated:true];
[ickImageViewController release];
2、利用UIViewController自身的presentModalViewController,进行跳转;调用dismissModalViewControllerAnimated方法可以返回。
复制代码代码如下:
PickImageViewController *ickImageViewController = [[PickImageViewController alloc] init];
[self presentModalViewController:ickImageViewController animated:YES];
//返回
[self dismissModalViewControllerAnimated:YES];
Present ViewController Modally
一、主要用途
弹出模态ViewController是IOS变成中很有用的一个技术,UIKit提供的一些专门用于模态显示的ViewController,如UIImagePickerController等。弹出模态ViewController主要使用于一下这几种情形:
1、收集用户输入信息
2、临时呈现一些内容
3、临时改变工作模式
4、相应设备方向变化(用于针对不同方向分别是想两个ViewController的情况)
5、显示一个新的view层级
这几种情形都会暂时中断程序正常的执行流程,主要作用是收集或者显示一些信息。
二、几个概念和常用设置
1、presenting view controller Vs presented view controller
当我们在view controller A中模态显示view controller B的时候,A就充当presenting view controller(弹出VC),而B就是presented view controller(被弹出VC)。官方文档建议这两者之间通过delegate实现交互,如果使用过UIImagePickerController从系统相册选取照片或者拍照,我们可以发现imagePickerController和弹出它的VC之间就是通过UIImagePickerControllerDelegate实现交互的。因此我们在实际应用用,最好也遵守这个原则,在被弹出的VC中定义delegate,然后在弹出VC中实现该代理,这样就可以比较方便的实现两者之间的交互。
2、Modal Presentation Styles(弹出风格)
通过设置presenting VC的modalPresentationStyle属性,我们可以设置弹出View Controller时的风格,有以下四种风格,其定义如下:
复制代码代码如下:
typedef enum {
UIModalPresentationFullScreen = 0,
UIModalPresentationPageSheet,
UIModalPresentationFormSheet,
UIModalPresentationCurrentContext,
} UIModalPresentationStyle;
UIModalPresentationFullScreen代表弹出VC时,presented VC充满全屏,如果弹出VC的wantsFullScreenLayout设置为YES的,则会填充到状态栏下边,否则不会填充到状态栏之下。
UIModalPresentationPageSheet代表弹出是弹出VC时,presented VC的高度和当前屏幕高度相同,宽度和竖屏模式下屏幕宽度相同,剩余未覆盖区域将会变暗并阻止用户点击,这种弹出模式下,竖屏时跟UIModalPresentationFullScreen的效果一样,横屏时候两边则会留下变暗的区域。
UIModalPresentationFormSheet这种模式下,presented VC的高度和宽度均会小于屏幕尺寸,presented VC居中显示,四周留下变暗区域。
UIModalPresentationCurrentContext这种模式下,presented VC的弹出方式和presenting VC的父VC的方式相同。
这四种方式在iPad上面统统有效,但在iPhone和iPod touch上面系统始终已UIModalPresentationFullScreen模式显示presented VC。
3、Modal Transition Style(弹出时的动画风格)
通过设置设置presented VC的modalTransitionStyle属性,我们可以设置弹出presented VC时场景切换动画的风格,其定义如下:
复制代码代码如下:
typedef enum {
UIModalTransitionStyleCoverVertical = 0,
UIModalTransitionStyleFlipHorizontal,
UIModalTransitionStyleCrossDissolve,
UIModalTransitionStylePartialCurl,
} UIModalTransitionStyle;
我们可以看到有从底部滑入,水平翻转进入,交叉溶解以及翻页这四种风格可选。这四种风格在不受设备的限制,即不管是iPhone还是iPad都会根据我们指定的风格显示转场效果。
4、Dismiss Modal ViewController(消失弹出的VC)
消失presented VC,我们可以通过调用以下两个函数中的任何一个来完成
复制代码代码如下:
dismissModalViewControllerAnimated: // 将要废弃,不赞成继续使用
dismissViewControllerAnimated:completion:
谁来调用这消失presented VC的这个方法:正确的做法是“谁污染谁治理”,即presenting VC调用上面的方法来取消presented VC的显示。这样做有一个好处,如果一个VC真不用户做的不同选择可能弹出不同的view controller,当不再需要显示被弹出的view controller的时候,直接调用[self dismissModalViewControllerAnimated]即可使之消失,而不用去关心其具体显示的哪一类view
controller。当然系统在这里做了优化,当我们在presented VC里面调用上面的方法的时候,系统会自动的将这个消息传递到相应的presenting VC中,这样就可以实现不管谁弹出了自己,当不再需要的时候直接将自己消失掉的功能。在应用中具体要采用那种要看具体情况,如果presented VC需要和presenting VC有数据传递的话,建议在presenting VC实现的代理函数中dismiss弹出的view controller。
从一个Controller跳转到另一个Controller时,一般有以下2种:
1、利用UINavigationController,调用pushViewController,进行跳转;这种采用压栈和出栈的方式,进行Controller的管理。调用popViewControllerAnimated方法可以返回。
复制代码代码如下:
PickImageViewController *ickImageViewController = [[PickImageViewController alloc] init];
[self.navigationController pushViewController: ickImageViewController animated:true];
[ickImageViewController release];
2、利用UIViewController自身的presentModalViewController,进行跳转;调用dismissModalViewControllerAnimated方法可以返回。
复制代码代码如下:
PickImageViewController *ickImageViewController = [[PickImageViewController alloc] init];
[self presentModalViewController:ickImageViewController animated:YES];
//返回
[self dismissModalViewControllerAnimated:YES];
Present ViewController Modally
一、主要用途
弹出模态ViewController是IOS变成中很有用的一个技术,UIKit提供的一些专门用于模态显示的ViewController,如UIImagePickerController等。弹出模态ViewController主要使用于一下这几种情形:
1、收集用户输入信息
2、临时呈现一些内容
3、临时改变工作模式
4、相应设备方向变化(用于针对不同方向分别是想两个ViewController的情况)
5、显示一个新的view层级
这几种情形都会暂时中断程序正常的执行流程,主要作用是收集或者显示一些信息。
二、几个概念和常用设置
1、presenting view controller Vs presented view controller
当我们在view controller A中模态显示view controller B的时候,A就充当presenting view controller(弹出VC),而B就是presented view controller(被弹出VC)。官方文档建议这两者之间通过delegate实现交互,如果使用过UIImagePickerController从系统相册选取照片或者拍照,我们可以发现imagePickerController和弹出它的VC之间就是通过UIImagePickerControllerDelegate实现交互的。因此我们在实际应用用,最好也遵守这个原则,在被弹出的VC中定义delegate,然后在弹出VC中实现该代理,这样就可以比较方便的实现两者之间的交互。
2、Modal Presentation Styles(弹出风格)
通过设置presenting VC的modalPresentationStyle属性,我们可以设置弹出View Controller时的风格,有以下四种风格,其定义如下:
复制代码代码如下:
typedef enum {
UIModalPresentationFullScreen = 0,
UIModalPresentationPageSheet,
UIModalPresentationFormSheet,
UIModalPresentationCurrentContext,
} UIModalPresentationStyle;
UIModalPresentationFullScreen代表弹出VC时,presented VC充满全屏,如果弹出VC的wantsFullScreenLayout设置为YES的,则会填充到状态栏下边,否则不会填充到状态栏之下。
UIModalPresentationPageSheet代表弹出是弹出VC时,presented VC的高度和当前屏幕高度相同,宽度和竖屏模式下屏幕宽度相同,剩余未覆盖区域将会变暗并阻止用户点击,这种弹出模式下,竖屏时跟UIModalPresentationFullScreen的效果一样,横屏时候两边则会留下变暗的区域。
UIModalPresentationFormSheet这种模式下,presented VC的高度和宽度均会小于屏幕尺寸,presented VC居中显示,四周留下变暗区域。
UIModalPresentationCurrentContext这种模式下,presented VC的弹出方式和presenting VC的父VC的方式相同。
这四种方式在iPad上面统统有效,但在iPhone和iPod touch上面系统始终已UIModalPresentationFullScreen模式显示presented VC。
3、Modal Transition Style(弹出时的动画风格)
通过设置设置presented VC的modalTransitionStyle属性,我们可以设置弹出presented VC时场景切换动画的风格,其定义如下:
复制代码代码如下:
typedef enum {
UIModalTransitionStyleCoverVertical = 0,
UIModalTransitionStyleFlipHorizontal,
UIModalTransitionStyleCrossDissolve,
UIModalTransitionStylePartialCurl,
} UIModalTransitionStyle;
我们可以看到有从底部滑入,水平翻转进入,交叉溶解以及翻页这四种风格可选。这四种风格在不受设备的限制,即不管是iPhone还是iPad都会根据我们指定的风格显示转场效果。
4、Dismiss Modal ViewController(消失弹出的VC)
消失presented VC,我们可以通过调用以下两个函数中的任何一个来完成
复制代码代码如下:
dismissModalViewControllerAnimated: // 将要废弃,不赞成继续使用
dismissViewControllerAnimated:completion:
谁来调用这消失presented VC的这个方法:正确的做法是“谁污染谁治理”,即presenting VC调用上面的方法来取消presented VC的显示。这样做有一个好处,如果一个VC真不用户做的不同选择可能弹出不同的view controller,当不再需要显示被弹出的view controller的时候,直接调用[self dismissModalViewControllerAnimated]即可使之消失,而不用去关心其具体显示的哪一类view
controller。当然系统在这里做了优化,当我们在presented VC里面调用上面的方法的时候,系统会自动的将这个消息传递到相应的presenting VC中,这样就可以实现不管谁弹出了自己,当不再需要的时候直接将自己消失掉的功能。在应用中具体要采用那种要看具体情况,如果presented VC需要和presenting VC有数据传递的话,建议在presenting VC实现的代理函数中dismiss弹出的view controller。
相关文章推荐
- iOS-制作Framework
- 安装CocoaPods
- iOS 限制输入字数完美解决方案
- ios 多线程之NSThread篇举例详解
- iOS9新特性 3DTouch 开发教程全解(含源码)
- iOS GCD NSOperation NSThread等多线程各种举例详解
- 手动内存管理转ARC项目实战
- iOS 应用 集成了Message(极光IM) 适配IPv6环境 提交应用时注意事项
- iOS:二叉树多级表格的使用,使用三方库TreeTableView-master实现对json解析数据的递归遍历整理成树状结构
- iOS APNS 推送相关博客
- 【iOS开发】如何使用enumerate开头的方法
- 解决angularJS在IOS上刷新黑屏的问题
- IOS开发中@2x图片等适应不同分辨率手机
- iOS开发中如何添加应用自己的字体
- CocoaPods更新到1.0.1遇到的坑
- IOS 证书申请、真机调试、外网下载测试
- iOS 设置行间距
- iOS-解析读取CSV文件,解析excel文件
- iOS- 什么是GitHub?关于它的自我介绍「初识 GitHub」
- iOS-内存泄露检测工具(MLeaksFinder)