转载请注明出处,原文网址: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
- <span style="font-family:Microsoft YaHei;font-size:18px;">//
- // MainViewController.h
- // MultiView-ModalView
- //
- // Created by Zhang Yanguang on 12-10-26.
- // Copyright (c) 2012年 MyCompanyName. All rights reserved.
- //
- #import <UIKit/UIKit.h>
- #import "InfoViewController.h"
- @interface MainViewController : UIViewController
- @property(nonatomic,retain)InfoViewController *infoViewController;
- -(IBAction)showInfoView:(id)sender;
- @end</span>
将操作showInfoView与MainViewController.xib中的button的Touch Up Inisde进行关联。
7、修改MainViewController.m,主要是实现showInfoView方法,如下
[cpp] view plaincopy
- <span style="font-family:Microsoft YaHei;font-size:18px;">//
- // MainViewController.m
- // MultiView-ModalView
- //
- // Created by Zhang Yanguang on 12-10-26.
- // Copyright (c) 2012年 MyCompanyName. All rights reserved.
- //
- #import "MainViewController.h"
- @interface MainViewController ()
- @end
- @implementation MainViewController
- @synthesize infoViewController;
- - (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
- {
- self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
- if (self) {
- // Custom initialization
- }
- return self;
- }
- - (void)viewDidLoad
- {
- [super viewDidLoad];
- // Do any additional setup after loading the view from its nib.
- //设置背景颜色
- self.view.backgroundColor = [UIColor grayColor];
- }
- -(void)dealloc{
- [infoViewController release];
- }
- -(IBAction)showInfoView:(id)sender{
- if(infoViewController == nil){
- infoViewController = [[InfoViewController alloc]initWithNibName:@"InfoViewController" bundle:nil];
- //NSLog(@"infoViewController is nil");
- }else{
- //NSLog(@"infoViewController is not nil");
- }
- infoViewController.modalTransitionStyle = UIModalTransitionStyleCoverVertical;
- //[self presentModalViewController:infoViewController animated:YES];//备注1
- [self presentViewController:infoViewController animated:YES completion:^{//备注2
- NSLog(@"show InfoView!");
- }];
- //presentedViewController
- NSLog(@"self.presentedViewController=%@",self.presentedViewController);//备注3
- }
- - (void)viewDidUnload
- {
- [super viewDidUnload];
- // Release any retained subviews of the main view.
- // e.g. self.myOutlet = nil;
- infoViewController = nil;
- }
- - (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
- {
- return (interfaceOrientation == UIInterfaceOrientationPortrait);
- }
- @end</span>
备注1、备注2:备注中的方法已经废弃,被备注2中的presentViewController代替;参数completion实现一个回调,当MainViewController的viewDidDisappear调用之后,该回调会被调用。
备注3:在MainViewController中调用self.presentedViewController,返回的是由MainViewController present出的视图控制器,在这里即是:infoViewController。
8、修改InfoViewController.h,如下
[cpp] view plaincopy
- <span style="font-family:Microsoft YaHei;font-size:18px;">//
- // InfoViewController.h
- // MultiView-ModalView
- //
- // Created by Zhang Yanguang on 12-10-26.
- // Copyright (c) 2012年 MyCompanyName. All rights reserved.
- //
- #import <UIKit/UIKit.h>
- @interface InfoViewController : UIViewController
- -(IBAction)backMainView:(id)sender;
- @end
- </span>
将操作backMainView与InfoViewController.xib中的button的Touch Up Inisde进行关联。
9、修改InfoViewController.m,主要是实现方法backMainView,如下
[cpp] view plaincopy
- <span style="font-family:Microsoft YaHei;font-size:18px;">//
- // InfoViewController.m
- // MultiView-ModalView
- //
- // Created by Zhang Yanguang on 12-10-26.
- // Copyright (c) 2012年 MyCompanyName. All rights reserved.
- //
- #import "InfoViewController.h"
- @interface InfoViewController ()
- @end
- @implementation InfoViewController
- - (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
- {
- self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
- if (self) {
- // Custom initialization
- }
- return self;
- }
- - (void)viewDidLoad
- {
- [super viewDidLoad];
- // Do any additional setup after loading the view from its nib.
- //设置背景颜色
- self.view.backgroundColor = [UIColor greenColor];
- }
- - (void)viewDidUnload
- {
- [super viewDidUnload];
- // Release any retained subviews of the main view.
- // e.g. self.myOutlet = nil;
- }
- -(IBAction)backMainView:(id)sender{
- NSLog(@"self.parentViewController=%@",self.parentViewController);
- //[self.parentViewController dismissViewControllerAnimated:YES completion:nil];//备注4
- /*
- 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.
- */
- NSLog(@"self.presentedViewController=%@",self.presentedViewController);
- //[self.presentedViewController dismissViewControllerAnimated:YES completion:nil]; //备注5
- NSLog(@"self.presentingViewController=%@",self.presentingViewController);
- //[self.presentingViewController dismissViewControllerAnimated:YES completion:nil];//备注6
- // Dismiss the current modal child. Uses a vertical sheet transition if animated. This method has been replaced by dismissViewControllerAnimated:completion:
- // It will be DEPRECATED, plan accordingly.
- //[self dismissModalViewControllerAnimated:YES];//备注7
- [self dismissViewControllerAnimated:YES completion:nil];//备注8
- }
- - (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
- {
- return (interfaceOrientation == UIInterfaceOrientationPortrait);
- }
- @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、编译、运行,效果如下
模态视图(转)