presentModalViewController和dismissModalViewControl

在实际开发中,若是要弹出视图:
咱们经常使用到 presentModalViewController方法和 dismissModalViewControllerAnimated方法。
presentModalViewController:弹出视图
dismissModalViewControllerAnimated隐藏 视图

贴代码:

弹出视图:

FeedbackViewController *feedbackViewController = [[FeedbackViewController alloc]initWithNibName:@"FeedbackViewController" bundle:nil]; app

    UINavigationController *navigationController = [[UINavigationControllerallocinitWithRootViewController:feedbackViewController]; 测试

    [self presentModalViewController:navigationController animated:YES]; this


隐藏视图:

[self dismissModalViewControllerAnimated:YES]; spa


关于这两个方法的几点说明:

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---- orm

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---- ci

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效果之间的衔接和转换。

用新的吧!与时俱进!

但愿对你有所帮助!
相关文章
相关标签/搜索