模态事图
#import "RootViewController.h"
#import "ModelViewController.h"
@interfaceRootViewController ()
@end
@implementation RootViewController
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {
// Custom initialization
}
returnself;
}
- (void)viewDidLoad
{
[superviewDidLoad];
UIButton *present = [UIButtonbuttonWithType:UIButtonTypeRoundedRect];
present.frame = CGRectMake(50, 50, 100, 60);
[present setTitle:@"present"forState:UIControlStateNormal];
[present addTarget:selfaction:@selector(presentModalVC) forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:present];
}
-(void)presentModalVC{
ModelViewController *mvc = [[ModelViewControlleralloc] init];
//自下向上推出(默认) UIModalTransitionStyleCoverVertical
//淡入淡出效果 UIModalTransitionStyleCrossDissolve
//左右翻转 UIModalTransitionStyleFlipHorizontal
//翻书效果 UIModalTransitionStylePartialCurl
// mvc.modalTransitionStyle = UIModalTransitionStylePartialCurl;
if ([[UIDevicecurrentDevice].systemVersionfloatValue] <6.0f) {
[selfpresentModalViewController:mvc animated:YES];
}else{
[selfpresentViewController:mvc animated:YEScompletion:^{
NSLog(@"call back");
}];
}
[mvc release];
}
- (void)didReceiveMemoryWarning
{
[superdidReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
@end
ModalViewController.m // // ModelViewController.m // test1 // // Created by wei8 on 13-5-27. // Copyright (c) 2013年 wei8. All rights reserved. // #import "ModelViewController.h" @interface ModelViewController () @end @implementation ModelViewController - (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil { self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil]; if (self) { // Custom initialization } return self; } - (void)viewDidLoad { [super viewDidLoad]; self.view.backgroundColor = [UIColor yellowColor]; UIButton *dismiss = [UIButton buttonWithType:UIButtonTypeRoundedRect]; dismiss.frame = CGRectMake(50, 50, 100, 60); [dismiss setTitle:@"dismiss" forState:UIControlStateNormal]; [dismiss addTarget:self action:@selector(dismissModalVC) forControlEvents:UIControlEventTouchUpInside]; [self.view addSubview:dismiss]; // Do any additional setup after loading the view. } -(void)dismissModalVC{ //判断系统版本以执行不同的视图消失方法 if ([[UIDevice currentDevice].systemVersion floatValue] < 6.0 ) { [self dismissModalViewControllerAnimated:YES]; }else{ [self dismissViewControllerAnimated:YES completion:^{ NSLog(@"dismiss call back"); }]; } } -(void)viewWillAppear:(BOOL)animated{ printf(__FUNCTION__); printf("\n"); } -(void)viewDidAppear:(BOOL)animated{ printf(__FUNCTION__); printf("\n"); } - (void)didReceiveMemoryWarning { [super didReceiveMemoryWarning]; // Dispose of any resources that can be recreated. } -(void)viewWillDisappear:(BOOL)animated{ printf(__FUNCTION__); printf("\n"); } -(void)viewDidDisappear:(BOOL)animated{ printf(__FUNCTION__); printf("\n"); } - (void)dealloc { printf(__FUNCTION__); printf("\n"); [super dealloc]; } @end
IOS6.0之后的模态视图呈现方法加了块语法。这里先判断系统版本以执行不同的方法。
模态视图消失时执行方法的顺序
-[ModelViewController viewWillAppear:] -[ModelViewController viewDidAppear:] 2013-05-27 22:58:11.434 test1[17382:11303] call back -[ModelViewController viewWillDisappear:] -[ModelViewController viewDidDisappear:] -[ModelViewController dealloc] 2013-05-27 22:58:15.908 test1[17382:11303] dismiss call back
//自下向上推出(默认) UIModalTransitionStyleCoverVertical
//淡入淡出效果 UIModalTransitionStyleCrossDissolve
//左右翻转 UIModalTransitionStyleFlipHorizontal
//翻书效果 UIModalTransitionStylePartialCurl
// mvc.modalTransitionStyle = UIModalTransitionStylePartialCurl;