一、主要用途

弹出模态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(弹出时的动画风格)

通过设置设置presenting 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。

《个人小实验》

  1. //
  2. //  ViewController.m
  3. //  ModalPresent
  4. //
  5. //  Created by Yongchao Zhang on 12-9-16.
  6. //  Copyright (c) 2012年 Yongchao Zhang. All rights reserved.
  7. //
  8. #import "ViewController.h"
  9. #import "TestViewController.h"
  10. @interface ViewController ()
  11. @end
  12. @implementation ViewController
  13. - (void)viewDidLoad
  14. {
  15. [super viewDidLoad];
  16. // Do any additional setup after loading the view, typically from a nib.
  17. testViewController = [[TestViewController alloc] init];
  18. for(int i = 0; i < 4; i++){
  19. UIButton *button = [UIButton buttonWithType:UIButtonTypeRoundedRect];
  20. //[btn setFrame:CGRectMake(i%ekLineOfButton*ekBtnSpace+10, i/ekLineOfButton*30+10, 25, 24)];
  21. [button setFrame:CGRectMake((i * 2 * 100)+50, 50, 100, 100)];
  22. button.tag = i;
  23. [button setTitle:@"弹出视图" forState:UIControlStateNormal];
  24. [button addTarget:self action:@selector(buttonClick:) forControlEvents:UIControlEventTouchUpInside];
  25. [self.view addSubview:button];
  26. }
  27. }
  28. //typedef enum {
  29. //    UIModalTransitionStyleCoverVertical = 0,
  30. //    UIModalTransitionStyleFlipHorizontal,
  31. //    UIModalTransitionStyleCrossDissolve,
  32. //#if __IPHONE_OS_VERSION_MAX_ALLOWED >= __IPHONE_3_2
  33. //    UIModalTransitionStylePartialCurl,
  34. //#endif
  35. //} UIModalTransitionStyle;
  36. //
  37. //typedef enum {
  38. //    UIModalPresentationFullScreen = 0,
  39. //#if __IPHONE_OS_VERSION_MAX_ALLOWED >= __IPHONE_3_2
  40. //    UIModalPresentationPageSheet,
  41. //    UIModalPresentationFormSheet,
  42. //    UIModalPresentationCurrentContext,
  43. //#endif
  44. //} UIModalPresentationStyle;
  45. -(void)buttonClick:(id)sender{
  46. UIButton *button = (UIButton *)sender;
  47. switch (button.tag) {
  48. case 1:
  49. {
  50. testViewController.modalPresentationStyle = UIModalPresentationFullScreen;
  51. testViewController.modalTransitionStyle = UIModalTransitionStyleCoverVertical;
  52. }
  53. break;
  54. case 2:
  55. {
  56. testViewController.modalPresentationStyle = UIModalPresentationPageSheet;
  57. testViewController.modalTransitionStyle = UIModalTransitionStyleFlipHorizontal;
  58. }
  59. break;
  60. case 3:
  61. {
  62. testViewController.modalPresentationStyle = UIModalPresentationFormSheet;
  63. testViewController.modalTransitionStyle = UIModalTransitionStyleCrossDissolve;
  64. }
  65. break;
  66. case 4:
  67. {
  68. testViewController.modalPresentationStyle = UIModalPresentationCurrentContext;
  69. testViewController.modalTransitionStyle = UIModalTransitionStylePartialCurl;
  70. }
  71. break;
  72. default:
  73. break;
  74. }
  75. [self presentModalViewController:testViewController animated:YES];
  76. }
  77. - (void)viewDidUnload
  78. {
  79. [super viewDidUnload];
  80. // Release any retained subviews of the main view.
  81. }
  82. - (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
  83. {
  84. return YES;
  85. }
  86. @end

要关闭只需要在关闭的时间里面加入:

[selfdismissModalViewControllerAnimated:YES];

即可实现。

Present ViewController Modally的更多相关文章

  1. Present ViewController Modally (转)

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

  2. ios控制器modal跳转

    1. http://www.cnblogs.com/smileEvday/archive/2012/05/29/presentModalViewController.html 2012年5月- Pre ...

  3. presenting view controller

    Present ViewController详解 Present ViewController Modally 一.主要用途 弹出模态ViewController是IOS变成中很有用的一个技术,UIK ...

  4. iOS UIKit:viewController之Present (3)

    弹出和转换view controller技术是一种快速且简单的方式将新view content展示在屏幕中.目前有两种方式弹出新的view controller:Present方式和segues方式. ...

  5. iOS中的场景转换机制的浅显分析

    目前Apple推荐的场景转换的方法有以下几个: 一般的跳转方法: presentViewController Discussion In a horizontally compact environm ...

  6. ios8 新增的 showViewController 和 showDetailViewController

    1.showViewController 先看看说明: You use this method to decouple the need to display a view controller fr ...

  7. ios8/sdk8/xcode6/iphone6(+)适配

    AppIcon https://developer.apple.com/library/ios/documentation/UserExperience/Conceptual/MobileHIG/Ic ...

  8. UINavigationController学习笔记

    http://site.douban.com/129642/widget/notes/5513129/note/187701199/ 1-view controllers的关系:Each custom ...

  9. iOS模拟器分辨率的问题(转载)

    转载地址:http://justsee.iteye.com/blog/2123545   不积跬步 无以至千里 不积小流 无以成江海   博客 微博 相册 收藏 留言 关于我     ios8/sdk ...

随机推荐

  1. apache2.4下载与安装

    step1 下载apache 百度“apache下载”,找到官网链接,如下 2. 点进去后选择 Files for microsoft windows,如下 3. 前三个任选一个,这里我们选第一个,如 ...

  2. URL encode 与 URL decode 的C语言实现

    转载自:http://blog.csdn.net/langeldep/article/details/6264058 本文代码为从PHP代码中修改而来,只保留了2个函数. int php_url_de ...

  3. BenchmarkDotNet

    .NET Core性能测试组件BenchmarkDotNet 支持.NET Framework Mono .NET Core 超强性能测试组件BenchmarkDotNet 支持Full .NET F ...

  4. Keras如何构造简单的CNN网络

    1. 导入各种模块 基本形式为: import 模块名 from 某个文件 import 某个模块 2. 导入数据(以两类分类问题为例,即numClass = 2) 训练集数据data 可以看到,da ...

  5. [BZOJ 1014] [JSOI2008] 火星人prefix 【Splay + Hash】

    题目链接:BZOJ - 1014 题目分析 求两个串的 LCP ,一种常见的方法就是 二分+Hash,对于一个二分的长度 l,如果两个串的长度为 l 的前缀的Hash相等,就认为他们相等. 这里有修改 ...

  6. 编写自己的C语言头文件

    一些初学C语言的人,不知道头文件(*.h文件)原来还可以自己写的.只知道调用系统库 函数时,要使用#include语句将某些头文件包含进去.其实,头文件跟.C文件一样,是可以自己写的.头文件是一种文本 ...

  7. QT内置的ICON资源

    QT内置的ICON资源保存在QStyle类里. 可以通过成员函数 QStyle::standardIcon 来获取. 保存的icon有: enum QStyle::StandardPixmap Thi ...

  8. scanf(),fscanf的详解

    我们这里只讨论fscanf(或者scanf)的格式,因为这些细节在其他贴里并没有涉及,阅读此文,你可以少走一些弯路.只讲结果,深层原因并不分析. FILE *pFile:float x1; char ...

  9. lua中求table长度

    关于lua table介绍,看以前的文章http://www.cnblogs.com/youxin/p/3672467.html. 官方文档是这么描述#的: 取长度操作符写作一元操作 #. 字符串的长 ...

  10. python的相对路径导入问题

    用python做项目,如果项目大了,或者想更好的管理程序,总是要使用包.包解决了命名冲突的问题. 今天在使用python的相对路径导入的时候,遇到了不少的问题. 包导入情形: src/    __in ...