当前位置: 首页 > 编程日记 > 正文

模态视图(转)

转载请注明出处,原文网址:http://blog.csdn.net/m_changgong/article/details/8127894 作者:张燕广

模态视图不是专门的某个类,而是通过视图控制器的presentViewController方法弹出的视图,我们称为模态视图。

  • 模态视图出现的场景一般是临时弹出的窗口,譬如:登录窗口;
  • 模态视图弹出时通过对视图对象的modalTransitionStyle来设置动画效果;
  • 在弹出的视图中使用dismissViewControllerAnimated方法关闭窗口。

实现的功能:1)通过弹出一个ModalView(模态视图),实现多视图;2)主界面上点击按钮弹出Info界面,在该界面上点击返回,返回到主界面。

关键词:多视图 MultiView模态视图 ModalView

1、创建一个Empty Application工程,命名为:MultiView-ModalView,如下图

2、选中工程中的Group MultiView-ModalView,然后按住CMD(Windows键)+N,新建视图控制器MainViewController,如下图

3、依照上步操作,新建视图控制器InfoViewController。

4、编辑MainViewController.xib,添加一个Label和Button,如下图

5、编辑InfoViewController.xib,添加一个Label和Button,如下图


6、修改MainViewController.h,如下

[cpp] view plaincopy
  1. <span style="font-family:Microsoft YaHei;font-size:18px;">//  
  2. //  MainViewController.h  
  3. //  MultiView-ModalView  
  4. //  
  5. //  Created by Zhang Yanguang on 12-10-26.  
  6. //  Copyright (c) 2012年 MyCompanyName. All rights reserved.  
  7. //  
  8. #import <UIKit/UIKit.h>  
  9. #import "InfoViewController.h"  
  10. @interface MainViewController : UIViewController
  11. @property(nonatomic,retain)InfoViewController *infoViewController;
  12. -(IBAction)showInfoView:(id)sender;
  13. @end</span>

将操作showInfoView与MainViewController.xib中的button的Touch Up Inisde进行关联。

7、修改MainViewController.m,主要是实现showInfoView方法,如下

[cpp] view plaincopy
  1. <span style="font-family:Microsoft YaHei;font-size:18px;">//  
  2. //  MainViewController.m  
  3. //  MultiView-ModalView  
  4. //  
  5. //  Created by Zhang Yanguang on 12-10-26.  
  6. //  Copyright (c) 2012年 MyCompanyName. All rights reserved.  
  7. //  
  8. #import "MainViewController.h"  
  9. @interface MainViewController ()
  10. @end
  11. @implementation MainViewController
  12. @synthesize infoViewController;
  13. - (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
  14. {
  15. self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
  16. if (self) {  
  17. // Custom initialization  
  18. }
  19. return self;  
  20. }
  21. - (void)viewDidLoad  
  22. {
  23. [super viewDidLoad];
  24. // Do any additional setup after loading the view from its nib.  
  25. //设置背景颜色  
  26. self.view.backgroundColor = [UIColor grayColor];
  27. }
  28. -(void)dealloc{  
  29. [infoViewController release];
  30. }
  31. -(IBAction)showInfoView:(id)sender{
  32. if(infoViewController == nil){  
  33. infoViewController = [[InfoViewController alloc]initWithNibName:@"InfoViewController" bundle:nil];  
  34. //NSLog(@"infoViewController is nil");  
  35. }else{  
  36. //NSLog(@"infoViewController is not nil");  
  37. }
  38. infoViewController.modalTransitionStyle = UIModalTransitionStyleCoverVertical;
  39. //[self presentModalViewController:infoViewController animated:YES];//备注1  
  40. [self presentViewController:infoViewController animated:YES completion:^{//备注2  
  41. NSLog(@"show InfoView!");  
  42. }];
  43. //presentedViewController  
  44. NSLog(@"self.presentedViewController=%@",self.presentedViewController);//备注3  
  45. }
  46. - (void)viewDidUnload  
  47. {
  48. [super viewDidUnload];
  49. // Release any retained subviews of the main view.  
  50. // e.g. self.myOutlet = nil;  
  51. infoViewController = nil;
  52. }
  53. - (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation  
  54. {
  55. return (interfaceOrientation == UIInterfaceOrientationPortrait);  
  56. }
  57. @end</span>

备注1、备注2:备注中的方法已经废弃,被备注2中的presentViewController代替;参数completion实现一个回调,当MainViewController的viewDidDisappear调用之后,该回调会被调用。

备注3:在MainViewController中调用self.presentedViewController,返回的是由MainViewController present出的视图控制器,在这里即是:infoViewController。

8、修改InfoViewController.h,如下

[cpp] view plaincopy
  1. <span style="font-family:Microsoft YaHei;font-size:18px;">//  
  2. //  InfoViewController.h  
  3. //  MultiView-ModalView  
  4. //  
  5. //  Created by Zhang Yanguang on 12-10-26.  
  6. //  Copyright (c) 2012年 MyCompanyName. All rights reserved.  
  7. //  
  8. #import <UIKit/UIKit.h>  
  9. @interface InfoViewController : UIViewController
  10. -(IBAction)backMainView:(id)sender;
  11. @end
  12. </span>

将操作backMainView与InfoViewController.xib中的button的Touch Up Inisde进行关联。

9、修改InfoViewController.m,主要是实现方法backMainView,如下

[cpp] view plaincopy
  1. <span style="font-family:Microsoft YaHei;font-size:18px;">//  
  2. //  InfoViewController.m  
  3. //  MultiView-ModalView  
  4. //  
  5. //  Created by Zhang Yanguang on 12-10-26.  
  6. //  Copyright (c) 2012年 MyCompanyName. All rights reserved.  
  7. //  
  8. #import "InfoViewController.h"  
  9. @interface InfoViewController ()
  10. @end
  11. @implementation InfoViewController
  12. - (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
  13. {
  14. self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
  15. if (self) {  
  16. // Custom initialization  
  17. }
  18. return self;  
  19. }
  20. - (void)viewDidLoad  
  21. {
  22. [super viewDidLoad];
  23. // Do any additional setup after loading the view from its nib.  
  24. //设置背景颜色  
  25. self.view.backgroundColor = [UIColor greenColor];
  26. }
  27. - (void)viewDidUnload  
  28. {
  29. [super viewDidUnload];
  30. // Release any retained subviews of the main view.  
  31. // e.g. self.myOutlet = nil;  
  32. }
  33. -(IBAction)backMainView:(id)sender{
  34. NSLog(@"self.parentViewController=%@",self.parentViewController);  
  35. //[self.parentViewController dismissViewControllerAnimated:YES completion:nil];//备注4  
  36. /* 
  37.      If this view controller is a child of a containing view controller (e.g. a navigation controller or tab bar 
  38.      controller,) this is the containing view controller.  Note that as of 5.0 this no longer will return the 
  39.      presenting view controller. 
  40.      */  
  41. NSLog(@"self.presentedViewController=%@",self.presentedViewController);  
  42. //[self.presentedViewController dismissViewControllerAnimated:YES completion:nil]; //备注5  
  43. NSLog(@"self.presentingViewController=%@",self.presentingViewController);  
  44. //[self.presentingViewController dismissViewControllerAnimated:YES completion:nil];//备注6  
  45. // Dismiss the current modal child. Uses a vertical sheet transition if animated. This method has been replaced by dismissViewControllerAnimated:completion:  
  46. // It will be DEPRECATED, plan accordingly.  
  47. //[self dismissModalViewControllerAnimated:YES];//备注7  
  48. [self dismissViewControllerAnimated:YES completion:nil];//备注8  
  49. }
  50. - (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation  
  51. {
  52. return (interfaceOrientation == UIInterfaceOrientationPortrait);  
  53. }
  54. @end</span>


备注4:不能正常工作,该代码不能实现返回到MainViewController的功能,因为MainViewController并不是InfoViewController的父视图控制器(父子试图控制器以后会讲到),该方法的注释如下:

/*
  If this view controller is a child of a containing view controller (e.g. a navigation controller or tab bar
  controller,) this is the containing view controller.  Note that as of 5.0 this no longer will return the
  presenting view controller.
*/

备注5:不能正常工作,代码也不能实现返回到MainViewController的功能,备注3中已解释过self.presentedViewController,在此处一定返回空。

备注6:可以正常工作,改代码可以实现返回到MainViewController的功能, self.presentingViewController返回的视图控制器是指present出当前视图控制器(即:infoViewController)的视图控制器,当然是MainViewController。

备注7、8:可以正常工作,改代码可以实现返回到MainViewController的功能,备注7中的方法已经废弃,已被备注8中的方法代替;现在要考虑的问题是:为什么[self dismissViewControllerAnimated:YES completion:nil]与[self.presentingViewController dismissViewControllerAnimated:YES completion:nil]实现了同样的功能?

类UIViewController的dismissViewControllerAnimated方法有一段注释如下:

The presenting view controller is responsible for dismissing the view controller it presented. If you call this method on the presented view controller itself, it automatically forwards the message to the presenting view controller.

什么意思呢?MainViewController把InforViewController 展示出来了,同样也要负责把InforViewController退出,如果直接在InforViewController中发出(调用)dismissViewControllerAnimated消息,这个消息会自动转给MainViewController,所以,在InforViewController中执行[self dismissViewControllerAnimated:YES completion:nil]与[self.presentingViewController dismissViewControllerAnimated:YES completion:nil]两种调用,效果是一样的,调用前者就等同于调用后者。建议用后者,更容易理解。

10、编译、运行,效果如下

转载于:https://www.cnblogs.com/hereiam/p/3813555.html

相关文章:

MHA二种高可用架构切换演练

高可用架构一 proxysqlkeepalivedmysqlmha优势&#xff0c;最大程序的降低脑裂风险&#xff0c;可以读写分离&#xff08;需要开启相应的插件支持&#xff09; 一、proxysql 1、安装 tar -zxvf proxysql.tar.gz -C /usr/local/chmod -R 700 /usr/local/proxysqlcd /usr/local/p…

如何关闭事件跟踪程序

最近经常遇到一些独享服务器用户反应自己的服务器联系万网工程师重起后&#xff0c;重新登陆时遇到的界面不知道该如何操作问题。当您看到此界面时&#xff0c;只需要在“注释”下面的空白处随意输入字符即可激活“确定”按钮&#xff0c;点击“确定”后可以进入系统。 这个界…

(C++)1015 德才论

#include<cstdio> #include<algorithm> #include<cstring> using namespace std; const int M 100000;struct Testee{char no[10];int de;int cai;int type;//第几类 }peo[M10];bool cmp(Testee a,Testee b){//比较顺序依次为总分&#xff0c;德分&#xf…

Vim命令相关

在shell中&#xff0c;记住一些常用的vim命令&#xff0c;会在操作时候事半功倍。 光标移动 h,j,k,l,h #表示往左&#xff0c;j表示往下&#xff0c;k表示往右&#xff0c;l表示往上 Ctrl f #上一页 Ctrl b #下一页 w, e, W, E #跳到单词的后面&#xff0c;小…

做科研的几点体会

刚刚开始做实验的时候&#xff0c;别人怎么说我就怎么做&#xff0c;每天在实验台旁干到深夜&#xff0c;以为这就是科研了。两个月过去&#xff0c;突然发现自己还在原地踏步。那种感觉&#xff0c;只能用”沮丧”来形 容。我开始置疑自己的行为和观念。感觉有种习惯的力量在束…

ICMP报文分析

一.概述&#xff1a;1. ICMP同意主机或路由报告差错情况和提供有关异常情况。ICMP是因特网的标准协议&#xff0c;但ICMP不是高层协议&#xff0c;而是IP层的协议。通常ICMP报文被IP层或更高层协议&#xff08;TCP或UDP&#xff09;使用。一些ICMP报文把差错报文返回给用户进…

(C++)1029 旧键盘

#include<cstdio> #include<cstring>const int M 80;//值得注意的地方是“按照发现顺序 ” //采取的最佳策略是&#xff0c;对于字符串1中的每一个字符&#xff0c;看在字符串2中是否出现int hashmap(char c){int res 0;if(0<c&&c<9){res c-0;}e…

深入理解 python 元类

一、什么的元类 # 思考&#xff1a; # Python 中对象是由实例化类得来的&#xff0c;那么类又是怎么得到的呢&#xff1f; # 疑问&#xff1a; # python 中一切皆对象&#xff0c;那么类是否也是对象&#xff1f;如果是&#xff0c;那么它又是那个类实例化而来的呢&…

使用.NET REACTOR制作软件许可证

使用.NET REACTOR制作软件许可证 原文:使用.NET REACTOR制作软件许可证软件下载地址&#xff1a;http://www.eziriz.com/downloads.htm 做一个简单的许可证系统&#xff0c;下面是具体步骤&#xff1a;1&#xff0c; OPEN ASSEMBLY打开项目可执行文件(debug文件夹里面exe文件…

(C++)CSP 201712-2 游戏

#include<cstdio> #include<algorithm> using namespace std;const int M 1000;int k;bool obsl(int x){if(x%k0||x%10k){return true;//淘汰 }else return false; }int main(){int n;//孩子的个数 scanf("%d%d",&n,&k);int i1;//现在报的数 in…

在wpf中运行EXE文件

最简单的方法&#xff1a;System.Diagnostics.Process.Start("路径");网上的其他方法&#xff1a; Process p new System.Diagnostics.Process(); p.StartInfo.FileName "路径"; p.StartInfo.Arguments ""; …

C语言程序试题

一个无向连通图G点上的哈密尔顿&#xff08;Hamiltion&#xff09;回路是指从图G上的某个顶点出发&#xff0c;经过图上所有其他顶点一次且仅一次&#xff0c;最后回到该顶点的路劲。一种求解无向图上哈密尔顿回路算法的基础实现如下&#xff1a; 假设图G存在一个从顶点V0出发的…

利用OWC创建图表的完美解决方案

http://onlytiancai.cnblogs.com/archive/2005/08/24/221761.html 转载于:https://www.cnblogs.com/Athrun/archive/2008/05/19/1202909.html

(C++)1020 月饼 简单贪心

#include<cstdio> #include<algorithm> using namespace std;int types,weight;//月饼的种类数 struct Mooncake{double totalPrice;double price;double weight;double sell;//卖出了多少 };bool cmp(Mooncake a,Mooncake b){return a.price>b.price; }int ma…

枚举,给枚举赋值

/**************枚举*****************/// public enum Colors{// Red,Yellow,Blue,Black,White// }// public static void main(String[] args) {// Colors c Colors.Yellow;// System.out.println(c);//输出枚举// System.out.println(c.ordinal());//输出枚举对应的序号…

青岛...沙尘暴!太可怕了~什么事儿都有!

受蒙古国和我国内蒙古地区出现沙尘暴天气的影响&#xff0c;28日&#xff0c;山东省青岛、烟台等地出现大范围浮尘天气&#xff0c;空气质量明显下降。 28日&#xff0c;一场大范围的浮尘天气影响到烟台&#xff0c;天空一片浑浊&#xff0c;能见度不足5公里&#xff0c;空气质…

面试题收集最新

Java高级程序员面试题------https://www.cnblogs.com/mengdou/p/7233398.html Java高级工程师面试题总结及参考答案-----https://www.cnblogs.com/java1024/p/8594784.html Java高级程序员&#xff08;5年左右&#xff09;面试的题目集----https://blog.csdn.net/fangqun663775…

(C++)1023 组个最小数 简单贪心

#include<cstdio> //#include<algorithm> //using namespace std; //用hash思想读入数字 //解决最高位放谁 //解决后面的位数 //输出 int main(){int key[10];for(int i0;i<10;i){scanf("%d",&key[i]);}//解决最高位for(int i1;i<10;i){if(ke…

Nginx 在centos linux 安装、部署完整步骤并测试通过

需要先装pcre, zlib&#xff0c;前者为了重写rewrite&#xff0c;后者为了gzip压缩。 1.选定源码目录 选定目录 /usr/local/ cd /usr/local/ 2.安装PCRE库 cd /usr/local/ wget http://exim.mirror.fr/pcre/pcre-8.02.tar.gz tar -zxvf pcre-8.02.tar.gz cd pcre-8.02 ./config…

Ubuntu16.04安装qt

5.11官方下载网站&#xff1a; http://download.qt.io/official_releases/qt/5.11/5.11.1/ 可以直接下载linux系统下的.run安装包&#xff1a; 安装方式&#xff1a;https://www.jb51.net/LINUXjishu/501994.html 切换到.run所在的目录&#xff0c;然后 第一步&#xff1a; chm…

好男人是怎么变坏的

十岁以前&#xff0c;就不说了&#xff0c;无非是淘气和不懂事。 十三、四岁的时候&#xff0c;开始对女孩有好感&#xff0c;但是那时候他离女孩远远的&#xff0c;并且以讨厌女孩自居&#xff0c;生怕被同伴嘲笑。 十五岁的时候&#xff0c;听到大人们说某某男人好花&#xf…

(C++)小明种苹果(续)

#include<cstdio>struct tree{int left;//剩余的果子数量bool fallfalse;//是否发生掉落int falls0;//这颗数前面的树&#xff08;包括自身&#xff09;发生掉落的次数 }trs[1000];int main(){int n;//树的总数scanf("%d",&n);for(int i0;i<n;i){//对于…

MySQL如何判别InnoDB表是独立表空间还是共享表空间

InnoDB采用按表空间&#xff08;tablespace)的方式进行存储数据, 默认配置情况下会有一个初始大小为10MB&#xff0c; 名字为ibdata1的文件&#xff0c; 该文件就是默认的表空间文件&#xff08;tablespce file&#xff09;&#xff0c;用户可以通过参数innodb_data_file_path对…

如何使用WindowsLiveWriter发文章

1.下载wlw最新版本http://download.microsoft.com/download/8/0/9/809604cd-bd08-42c8-b590-49c332059e64/writer.msi 2.在菜单中选择“Weblog”&#xff0c;然后选择“Another Weblog Service”。如图一 &#xff08;图一&#xff09; 3.在Weblog Homepage URL中输入你的Blog主…

很多学ThinkPHP的新手会遇到的问题

在模板传递变量的时候&#xff0c;很多视频教程都使用$v.channel的方式&#xff0c;如下&#xff1a; <a href"{:U(Chat/set,array(id>$v.channel))}" title"设置" class"btn btn-mini tip"> 这会导致URL在解析的时候出现问题&#xff…

(C++)1040 有几个PAT

#include<cstdio> #include<cstring> const int MOD 1000000007; const int maxn 100010;int main(){char str[maxn];scanf("%s",str);int len strlen(str);//数出每个元素左侧的P的个数int leftnumP[maxn];leftnumP[0] 0;for(int i1;i<len;i){if…

C#进行Visio二次开发之电气线路停电分析逻辑

停电分析&#xff0c;顾名思义&#xff0c;是对图纸进行停电的逻辑分析。在电气化线路中&#xff0c;一条线路是从一个电源出来&#xff0c;连接着很多很多的设备的&#xff0c;进行停电分析&#xff0c;有两个重要的作用&#xff1a;一是看图纸上的Shape元件是否连接正常&…

红芯丑闻揭秘者 Touko 专访 | 关于红芯丑闻的更多内幕……

专栏 | 九章算法 网址 | www.jiuzhang.com ❤ 红芯事件 近日&#xff0c;一则《自主研发的国产浏览器内核&#xff0c;红芯宣布获2.5亿C轮融资》的讯息再次将“国产自主创新”这一话题推向高潮&#xff0c;希冀之声群起。然好景不长&#xff0c;网友Touko在将红芯浏览器的exe文…

数学图形(1.20)N叶草

有N个叶子的草 相关软件参见:数学图形可视化工具,使用自己定义语法的脚本代码生成数学图形.该软件免费开源.QQ交流群: 367752815 vertices 1000 t from 0 to (2*PI) r 10 n rand_int2(3, 10) p 1 cos(n*t) sin(n*t)^2 x p*cos(t) y p*sin(t) N叶草面_1 vertices D1:5…

(C++)1045 快速排序 非满分

#include<cstdio>const int maxn100010; //思路&#xff0c;从第一个元素开始&#xff0c;假设其是主元&#xff0c;然后用two pointers方法&#xff0c;看有没有进行交换&#xff0c;进行了则不是 int main(){int iszy[maxn]{0};//0表示可以是主元&#xff0c;1表示一定…