iPhone开发【八】多视图技术总结之一:ModalView(模态视图)

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

实现的功能: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,如下

//
//  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
将操作showInfoView与MainViewController.xib中的button的Touch Up Inisde进行关联。

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

//
//  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

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

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

8、修改InfoViewController.h,如下

//
//  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
将操作backMainView与InfoViewController.xib中的button的Touch Up Inisde进行关联。

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

//
//  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

备注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、编译、运行,效果如下


点击下载本文源代码

  • 1
    点赞
  • 12
    收藏
    觉得还不错? 一键收藏
  • 6
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 6
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值