模态视图(转)

转载请注明出处,原文网址: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.   
  9. #import <UIKit/UIKit.h>  
  10. #import "InfoViewController.h"  
  11. @interface MainViewController : UIViewController  
  12. @property(nonatomic,retain)InfoViewController *infoViewController;  
  13.   
  14. -(IBAction)showInfoView:(id)sender;  
  15. @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.   
  9. #import "MainViewController.h"  
  10.   
  11. @interface MainViewController ()  
  12.   
  13. @end  
  14.   
  15. @implementation MainViewController  
  16. @synthesize infoViewController;  
  17.   
  18. - (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil  
  19. {  
  20.     self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];  
  21.     if (self) {  
  22.         // Custom initialization  
  23.     }  
  24.     return self;  
  25. }  
  26.   
  27. - (void)viewDidLoad  
  28. {  
  29.     [super viewDidLoad];  
  30.     // Do any additional setup after loading the view from its nib.  
  31.     //设置背景颜色  
  32.     self.view.backgroundColor = [UIColor grayColor];  
  33. }  
  34.   
  35. -(void)dealloc{  
  36.     [infoViewController release];  
  37. }  
  38.   
  39. -(IBAction)showInfoView:(id)sender{  
  40.     if(infoViewController == nil){  
  41.         infoViewController = [[InfoViewController alloc]initWithNibName:@"InfoViewController" bundle:nil];  
  42.         //NSLog(@"infoViewController is nil");  
  43.     }else{  
  44.         //NSLog(@"infoViewController is not nil");  
  45.     }  
  46.     infoViewController.modalTransitionStyle = UIModalTransitionStyleCoverVertical;  
  47.       
  48.     //[self presentModalViewController:infoViewController animated:YES];//备注1  
  49.     [self presentViewController:infoViewController animated:YES completion:^{//备注2  
  50.         NSLog(@"show InfoView!");  
  51.     }];  
  52.       
  53.     //presentedViewController  
  54.     NSLog(@"self.presentedViewController=%@",self.presentedViewController);//备注3  
  55. }  
  56.   
  57. - (void)viewDidUnload  
  58. {  
  59.     [super viewDidUnload];  
  60.     // Release any retained subviews of the main view.  
  61.     // e.g. self.myOutlet = nil;  
  62.     infoViewController = nil;  
  63. }  
  64.   
  65. - (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation  
  66. {  
  67.     return (interfaceOrientation == UIInterfaceOrientationPortrait);  
  68. }  
  69.   
  70. @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.   
  9. #import <UIKit/UIKit.h>  
  10.   
  11. @interface InfoViewController : UIViewController  
  12.   
  13. -(IBAction)backMainView:(id)sender;  
  14. @end  
  15. </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.   
  9. #import "InfoViewController.h"  
  10.   
  11. @interface InfoViewController ()  
  12.   
  13. @end  
  14.   
  15. @implementation InfoViewController  
  16.   
  17. - (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil  
  18. {  
  19.     self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];  
  20.     if (self) {  
  21.         // Custom initialization  
  22.     }  
  23.     return self;  
  24. }  
  25.   
  26. - (void)viewDidLoad  
  27. {  
  28.     [super viewDidLoad];  
  29.     // Do any additional setup after loading the view from its nib.  
  30.     //设置背景颜色  
  31.     self.view.backgroundColor = [UIColor greenColor];  
  32. }  
  33.   
  34. - (void)viewDidUnload  
  35. {  
  36.     [super viewDidUnload];  
  37.     // Release any retained subviews of the main view.  
  38.     // e.g. self.myOutlet = nil;  
  39. }  
  40.   
  41. -(IBAction)backMainView:(id)sender{   
  42.     NSLog(@"self.parentViewController=%@",self.parentViewController);  
  43.     //[self.parentViewController dismissViewControllerAnimated:YES completion:nil];//备注4  
  44.       
  45.     /* 
  46.      If this view controller is a child of a containing view controller (e.g. a navigation controller or tab bar 
  47.      controller,) this is the containing view controller.  Note that as of 5.0 this no longer will return the 
  48.      presenting view controller. 
  49.      */  
  50.     NSLog(@"self.presentedViewController=%@",self.presentedViewController);  
  51.     //[self.presentedViewController dismissViewControllerAnimated:YES completion:nil]; //备注5  
  52.       
  53.     NSLog(@"self.presentingViewController=%@",self.presentingViewController);  
  54.     //[self.presentingViewController dismissViewControllerAnimated:YES completion:nil];//备注6  
  55.       
  56.     // Dismiss the current modal child. Uses a vertical sheet transition if animated. This method has been replaced by dismissViewControllerAnimated:completion:  
  57.     // It will be DEPRECATED, plan accordingly.  
  58.     //[self dismissModalViewControllerAnimated:YES];//备注7  
  59.     [self dismissViewControllerAnimated:YES completion:nil];//备注8  
  60. }  
  61.   
  62. - (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation  
  63. {  
  64.     return (interfaceOrientation == UIInterfaceOrientationPortrait);  
  65. }  
  66.   
  67. @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