在实际开发中,如果要弹出视图:
我们常用到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的使用总结的更多相关文章

  1. IOS开发基础知识碎片-导航

    1:IOS开发基础知识--碎片1 a:NSString与NSInteger的互换 b:Objective-c中集合里面不能存放基础类型,比如int string float等,只能把它们转化成对象才可 ...

  2. IOS开发基础知识--碎片6

    三十三:IOS多视图跳转方法 第一种: 跳转:[self presentModalViewController:control animated:YES]; 返回:[self dismissModal ...

  3. 放弃iOS4,拥抱iOS5

      前言 苹果在2011年的WWDC大会上发布了iOS5,不过考虑到要支持iOS4.x的系统,大多数App都无法使用iOS5的新特性.现在将近1年半过去了,从我们自己的App后台的统计数据.一些第三方 ...

  4. self dismissModalViewControllerAnimated:YES 无效(dismissviewcontrolleranimated无效)

    作为一个viewController(VC),想要消失的时候可以从parent VC里面调用dismissModalViewControllerAnimated来消去改VC,也可以在该VC里面手动调用 ...

  5. 模态显示PresentModalViewController

    1.主要用途 弹出模态ViewController是IOS变成中很有用的一个技术,UIKit提供的一些专门用于模态显示的ViewController,如UIImagePickerController等 ...

  6. 弹出视图/弹出模态presentViewController与presentModalViewController

    一.主要用途 弹出模态ViewController是IOS变成中很有用的一个技术,UIKit提供的一些专门用于模态显示的ViewController,如UIImagePickerController等 ...

  7. IOS疯狂基础之模态显示PresentModalViewController(转)

    转自:http://blog.csdn.net/wudizhukk/article/details/8553554 -(void)buttonDown:(id)sender{ ViewTwo *two ...

  8. pushViewController addSubview presentModalViewController视图切换

    1.pushViewController和popViewController来进行视图切换,首先要确保根视图是NavigationController,不然是不可以用的, pushViewContro ...

  9. IOS开发~灵活使用 dismissViewControllerAnimated / dismissModalViewControllerAnimated

    当遇到: A presentViewController B ,  B presentViewController C,  C presentViewController D,问如何从D一下子回到A, ...

随机推荐

  1. UVA 140 Bandwidth (dfs 剪枝 映射)

    题意: 给定一个n个结点的图G和一个结点的排列, 定义结点i的带宽b(i)为i和相邻结点在排列中的最远距离, 所有b(i)的最大值就是这个图的带宽, 给定G, 求让带宽最小的结点排列. 给定的图 n ...

  2. Gram-Schmidt向量正交化

    正交:向量的内积为0,即相互垂直. 假如存在向量a,b确定一个平面空间,但是a,b向量并不垂直,如下图. 现在要在该平面内找出2个垂直的向量确定该平面: b和e垂直,接下来求解e: 根据向量计算法则: ...

  3. 关于SQL Server 的限制

    经常被人问到关于SQL Server 的连接数限制, 或者最大的文件大小, 或者标准版和企业版的区别,以及Express上的 其实这些问题都可以在MSDN 上直接找到 SQL Server 2014 ...

  4. NYOJ-770仿射密码,乘数密码与移位密码的结合;

    仿射密码 时间限制:1000 ms  |  内存限制:65535 KB 难度:1 ->   Link   <- 和乘数密码差不多: 加密算法:Ek(m)=(k1*m+k2)%q;gcd(k ...

  5. 面向对象:继承(经典类&新式类继承原理、属性查找)、派生

    继承: 继承是指类与类之间的关系,是一种“什么”是“什么”的关系. 继承的功能之一就是用来解决代码重用问题 继承是一种创建新类的方式,在Python中,新建的类可以继承一个或多个父类,父类又可以称为基 ...

  6. msp430入门学习06

    msp430的IO端口的第一功能 msp430入门学习

  7. Java并发包——线程通信

    Java并发包——线程通信 摘要:本文主要学习了Java并发包里有关线程通信的一些知识. 部分内容来自以下博客: https://www.cnblogs.com/skywang12345/p/3496 ...

  8. RabbitMQ环境搭建教程收集(待实践)

    先收集,后续再实践. http://blog.csdn.net/zyz511919766/article/details/41896823 http://blog.chinaunix.net/uid- ...

  9. Redis集群方案收集

    说明: 如果不考虑客户端分片去实现集群,那么市面上基本可以说就三种方案最成熟,它们分别如下所示: 系统 贡献者 是否官方Redis实现 编程语言 Twemproxy Twitter 是 C Redis ...

  10. SHARP 加粉

    http://v.ku6.com/show/7ufwm7pqfF8D_f13IdCaag...html