[ios]presentModalViewController和dismissModalViewControllerAnimated汇总
2016-04-26 18:12
627 查看
在实际开发中,如果要弹出视图:
我们常用到presentModalViewController方法和dismissModalViewControllerAnimated方法。
presentModalViewController:弹出视图
dismissModalViewControllerAnimated:隐藏视图
贴代码:
弹出视图:
FeedbackViewController *feedbackViewController = [[FeedbackViewController alloc]initWithNibName:@"FeedbackViewController" bundle:nil];
UINavigationController *navigationController = [[UINavigationController alloc]initWithRootViewController:feedbackViewController];
[self presentModalViewController:navigationController animated:YES];
隐藏视图:
[self dismissModalViewControllerAnimated:YES];
关于这两个方法的几点说明:
1.iPhone上弹出/隐藏 视图时,使用为全屏模式
On iPhone and iPod touch devices, the
view of modalViewController is always presented full screen.
2.搞清楚谁是presenting,谁是presented
如果A弹出B,那么A为presenting,B为presented。
3.隐藏视图的策略
The presenting view controller is
responsible for dismissing the view controller it presented. If you call this
method on the presented view controller itself, however, it automatically
forwards the message to the presenting view controller.
我们假如A弹出B
就是说,A负责隐藏B;如果我们在B中调用dismissModalViewControllerAnimated方法,那么编译器,自动将消息发送给A。
等等,什么消息?
简单的理解,当执行presentModalViewController:方法:在A弹出B时:
执行A的viewWillDisappear方法,
通知B执行自己的viewWillAppear方法和viewDidAppear方法
执行A的viewDidDisappear方法
当执行dismissModalViewControllerAnimated方法:隐藏B时:
执行B的viewWillDisappear
通知A执行自己的viewWillAppear方法和viewDidAppear方法
执行B的viewDidDisappear方法
以下我做了个测试来输出一轮AB切换:
A:More
B:Feed
2012-12-27 14:01:23.666 WTV[1627:11303]
-More--viewWillDisappear----
2012-12-27 14:01:23.672 WTV[1627:11303]
-Feed--viewWillAppear----
2012-12-27 14:01:24.086 WTV[1627:11303]
-Feed--viewDidAppear----
2012-12-27 14:01:24.087 WTV[1627:11303]
-More--viewDidDisappear----
2012-12-27 14:01:25.745 WTV[1627:11303]
-Feed--viewWillDisappear----
2012-12-27 14:01:25.745 WTV[1627:11303]
-More--viewWillAppear----
2012-12-27 14:01:26.156 WTV[1627:11303]
-More--viewDidAppear----
2012-12-27 14:01:26.157 WTV[1627:11303]
-Feed--viewDidDisappear----
当我们信心慢慢,庆幸我们可以了解了这两个方法时,悲剧发生了:
4.苹果官方已经把这两个方法 Deprecated in
iOS 6.0. 了
- (void)presentModalViewController:(UIViewController
*)modalViewController animated:(BOOL)animated;
- (void)dismissModalViewControllerAnimated:(BOOL)animated;
取而代之的是:
[self presentViewController:navigationController
animated:YES
completion:^(void){
// Code
}];
[self dismissViewControllerAnimated:YES
completion:^(void){
// Code
}];
新接口的差别是提供了一个参数,允许你传入一个block。这个block的回调方法在VC的viewWillDisappear方法后调用。也就是被隐藏的VC对象被释放后运行回调。
这样做的好处:可以方便做多个UI效果之间的衔接和转换。
我们常用到presentModalViewController方法和dismissModalViewControllerAnimated方法。
presentModalViewController:弹出视图
dismissModalViewControllerAnimated:隐藏视图
贴代码:
弹出视图:
FeedbackViewController *feedbackViewController = [[FeedbackViewController alloc]initWithNibName:@"FeedbackViewController" bundle:nil];
UINavigationController *navigationController = [[UINavigationController alloc]initWithRootViewController:feedbackViewController];
[self presentModalViewController:navigationController animated:YES];
隐藏视图:
[self dismissModalViewControllerAnimated:YES];
关于这两个方法的几点说明:
1.iPhone上弹出/隐藏 视图时,使用为全屏模式
On iPhone and iPod touch devices, the
view of modalViewController is always presented full screen.
2.搞清楚谁是presenting,谁是presented
如果A弹出B,那么A为presenting,B为presented。
3.隐藏视图的策略
The presenting view controller is
responsible for dismissing the view controller it presented. If you call this
method on the presented view controller itself, however, it automatically
forwards the message to the presenting view controller.
我们假如A弹出B
就是说,A负责隐藏B;如果我们在B中调用dismissModalViewControllerAnimated方法,那么编译器,自动将消息发送给A。
等等,什么消息?
简单的理解,当执行presentModalViewController:方法:在A弹出B时:
执行A的viewWillDisappear方法,
通知B执行自己的viewWillAppear方法和viewDidAppear方法
执行A的viewDidDisappear方法
当执行dismissModalViewControllerAnimated方法:隐藏B时:
执行B的viewWillDisappear
通知A执行自己的viewWillAppear方法和viewDidAppear方法
执行B的viewDidDisappear方法
以下我做了个测试来输出一轮AB切换:
A:More
B:Feed
2012-12-27 14:01:23.666 WTV[1627:11303]
-More--viewWillDisappear----
2012-12-27 14:01:23.672 WTV[1627:11303]
-Feed--viewWillAppear----
2012-12-27 14:01:24.086 WTV[1627:11303]
-Feed--viewDidAppear----
2012-12-27 14:01:24.087 WTV[1627:11303]
-More--viewDidDisappear----
2012-12-27 14:01:25.745 WTV[1627:11303]
-Feed--viewWillDisappear----
2012-12-27 14:01:25.745 WTV[1627:11303]
-More--viewWillAppear----
2012-12-27 14:01:26.156 WTV[1627:11303]
-More--viewDidAppear----
2012-12-27 14:01:26.157 WTV[1627:11303]
-Feed--viewDidDisappear----
当我们信心慢慢,庆幸我们可以了解了这两个方法时,悲剧发生了:
4.苹果官方已经把这两个方法 Deprecated in
iOS 6.0. 了
- (void)presentModalViewController:(UIViewController
*)modalViewController animated:(BOOL)animated;
- (void)dismissModalViewControllerAnimated:(BOOL)animated;
取而代之的是:
[self presentViewController:navigationController
animated:YES
completion:^(void){
// Code
}];
[self dismissViewControllerAnimated:YES
completion:^(void){
// Code
}];
新接口的差别是提供了一个参数,允许你传入一个block。这个block的回调方法在VC的viewWillDisappear方法后调用。也就是被隐藏的VC对象被释放后运行回调。
这样做的好处:可以方便做多个UI效果之间的衔接和转换。
相关文章推荐
- iOS-正则表达式的使用
- iOS--串口通讯初始化
- 【转载】Xcode 4.1~4.6 + iOS 5、iOS 6免证书(iDP)开发+真机调试+生成IPA全攻略
- 如何让iOS应用程序访问到 dev下的设备
- ios6.0横竖屏切换问题解决
- 2011年冬季最新斯坦福大学公开课:iOS程序开发教程【中文字幕】
- ios和配件进行通讯[转]
- ios开发 获取系统时间
- iOS 常用代码汇总(持续更新)
- iOS开发实例-七杯茶奶茶点餐系统
- iOS 编译百度地图静态库-合二为一
- iOS-coverFlow效果简单实现
- iOS home键直接退出
- iOS单例模式使用
- 截取部分图片并显示-ios例子[转载]
- 关于IOS获取本地通讯录信息(包含iOS9.0前后)
- 一步一步构建iOS持续集成:Jenkins+GitLab+蒲公英+FTP
- iOS 开发谨慎使用成员变量
- 解决IOS浏览器中文乱码问题
- iOS 划线总结