在实际开发中,如果要弹出视图:
我们常用到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. Python 网络编程介绍

    网络编程介绍 1. 目标: 编写一个C/S架构的软件 C/S: Client ----------- 基于网络 --------- Server B/S: Browser -------- 基于网络 ...

  2. Linux修改启动界面、分辨率

    初识Linux 初识Linux(Centos 7.x),积累一些小技巧. 修改命令行界面的分辨率 # 备份配置文件 # 有些系统路径是/boot/grub...或者/boot/grub/menu.ls ...

  3. linux纯字符界面不支持中文

    [2017-01-17] linux纯字符界面不支持中文

  4. cookie和session的区别及session的生命周期

    这些都是基础知识,不过有必要做深入了解.先简单介绍一下. 二者的定义: 当你在浏览网站的时候,WEB 服务器会先送一小小资料放在你的计算机上,Cookie 会帮你在网站上所打的文字或是一些选择,都纪录 ...

  5. [Android] java代码无错误,但跳转失败

    今天在调代码的时候,出现了这样的问题,我晕了半天,才找到解决办法. 查看日志发现:Initialize Binary Program Cache: Load Failed 从来没见过这种问题,Java ...

  6. UVA 1596 Bug Hunt (大模拟 栈)

    题意: 输入并模拟执行一段程序,输出第一个bug所在的行. 每行程序有两种可能: 数组定义: 格式为arr[size]. 例如a[10]或者b[5],可用下标分别是0-9和0-4.定义之后所有元素均为 ...

  7. //……关于TCP三次握手与四次挥手

    TCP的概述 TCP把连接作为最基本的对象,每一条TCP连接都有两个端点,这种断点我们叫作套接字(socket),它的定义为端口号拼接到IP地址即构成了套接字,例如,若IP地址为192.3.4.16 ...

  8. Overload重載和Override重写的区别。Overloaded的方法是否可以改变返回值的类型?

    Overload是重载的意思,Override是覆盖的意思,也就是重写. 重载Overload表示同一个类中可以有多个名称相同的方法,但这些方法的参数列表各不相同(即参数个数或类型不同). 重写Ove ...

  9. Phong 光照模型(镜面反射)

    Phong 光照模型 镜面反射(高光),是光线经过物体表面,反射到视野中,当反射光线与人的眼睛看得方向平行时,强度最大,高光效果最明显,夹角为90度时,强度最小. specular = I*R*V: ...

  10. java程序调用bat脚本

    运用Java程序控制某个应用程序的运行(以网易云音乐为例),步骤如下 1.建立bat文件分别是start.bat(控制程序的运行)和kill.bat(控制程序的结束): start.bat 的内容如下 ...