ViewController生命周期的生命周期相信大家也都比较熟悉了, 不熟悉的可以看下ViewController生命周期
那么在push跳转和模态跳转中ViewController会是什么样的生命周期:
笔者做了一个打印的测试(很好理解) push 和 模态对于ViewController生命周期方法执行顺序还是有点区别的O(∩_∩)O哈哈~
1. VC1pushVC2或者模态到VC2的时候, VC1不会dealloc(不会被释放).
2. VC2popVC1或者dismissV1的时候, VC2会被dealloc掉.
3. push和模态执行顺序上有点区别
具体代码和打印结果如下:
ViewController.m 文件
//
// ViewController.m
// UIControllerLifeCicle
//
// Created by 帝炎魔 on 16/5/5.
// Copyright © 2016年 帝炎魔. All rights reserved.
//
#import "ViewController.h"
#import "SecondViewController.h"
@interface ViewController ()
@end
@implementation ViewController
- (void)viewDidAppear:(BOOL)animated
{
[super viewDidAppear:animated];
NSLog(@"VC1 viewDidAppear 已经出现");
}
- (void)viewWillAppear:(BOOL)animated
{
[super viewWillAppear:animated];
NSLog(@"VC1 viewWillAppear 将要出现");
}
- (void)loadView
{
[super loadView];
NSLog(@"VC1.view loadView 为nil的时候 创建view");
}
-(void)viewDidDisappear:(BOOL)animated
{
[super viewDidDisappear:animated];
NSLog(@"VC1 viewDidDisappear 已经消失");
}
-(void)viewWillDisappear:(BOOL)animated
{
[ super viewWillDisappear:animated];
NSLog(@"VC1 viewWillDisappear 将要消失");
}
- (void)dealloc
{
NSLog(@"VC1 dealloc 被dealloc掉");
}- (void)viewDidLoad {
[super viewDidLoad];
NSLog(@"-----VC1.view viewDidLoad");
self.view.backgroundColor = [UIColor whiteColor];
UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
button.frame = CGRectMake(100, 100, 100, 100);
button.backgroundColor = [UIColor redColor];
[button setTitle:@"pushVC2" forState:UIControlStateNormal];
[button setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
[button addTarget:self action:@selector(pushVC2) forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:button];
UIButton *button2 = [UIButton buttonWithType:UIButtonTypeCustom];
button2.frame = CGRectMake(100, 300, 100, 100);
[button2 setTitle:@"模态VC2" forState:UIControlStateNormal];
[button2 setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
button2.backgroundColor = [UIColor blueColor];
[button2 addTarget:self action:@selector(presentVC2) forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:button2];
// Do any additional setup after loading the view, typically from a nib.
}
- (void)pushVC2
{
NSLog(@"点击跳转===== VC1 push到 VC2");
SecondViewController *secondVC = [[SecondViewController alloc] init];
[self.navigationController pushViewController:secondVC animated:YES];
}
- (void)presentVC2
{
NSLog(@"点击跳转===== VC1 模态到 VC2");
SecondViewController *secondVC = [[SecondViewController alloc] init];
[self presentViewController:secondVC animated:YES completion:nil];
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
@end
SecondViewController.m 文件
//
// SecondViewController.m
// UIControllerLifeCicle
//
// Created by 帝炎魔 on 16/5/5.
// Copyright © 2016年 帝炎魔. All rights reserved.
//
#import "SecondViewController.h"
@interface SecondViewController ()
@end
@implementation SecondViewController
- (void)viewDidAppear:(BOOL)animated
{
[super viewDidAppear:animated];
NSLog(@"------VC2 viewDidAppear 已经出现");
}
- (void)viewWillAppear:(BOOL)animated
{
[super viewWillAppear:animated];
NSLog(@"------VC2 viewWillAppear 将要出现");
}
- (void)loadView
{
[super loadView];
NSLog(@"-----VC2.view loadView 为nil的时候 创建view");
}
-(void)viewDidDisappear:(BOOL)animated
{
[super viewDidDisappear:animated];
NSLog(@"------VC2 viewDidDisappear 已经消失");
}
-(void)viewWillDisappear:(BOOL)animated
{
[ super viewWillDisappear:animated];
NSLog(@"-----VC2 viewWillDisappear 将要消失");
}
- (void)dealloc
{
NSLog(@"------VC2 dealloc 被dealloc掉");
}
- (void)viewDidLoad {
[super viewDidLoad];
NSLog(@"-----VC2.view viewDidLoad");
self.view.backgroundColor = [UIColor whiteColor];
UIBarButtonItem *leftItem = [[UIBarButtonItem alloc] initWithTitle:@"返回" style:UIBarButtonItemStylePlain target:self action:@selector(popVC1)];
self.navigationItem.leftBarButtonItem = leftItem;
UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
button.frame = CGRectMake(100, 100, 100, 100);
button.backgroundColor = [UIColor redColor];
[button setTitle:@"dismissVC1" forState:UIControlStateNormal];
[button setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
[button addTarget:self action:@selector(dismissVC1) forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:button];
// Do any additional setup after loading the view.
}
- (void)popVC1
{
NSLog(@"点击返回按钮=========VC2 pop 到 VC1");
[self.navigationController popViewControllerAnimated:YES];
}
- (void)dismissVC1
{
[self dismissViewControllerAnimated:YES completion:nil];
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
/*
#pragma mark - Navigation
// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
// Get the new view controller using [segue destinationViewController].
// Pass the selected object to the new view controller.
}
*/
@end
下面分别看下打印结果
1. VC1push到VC2,并pop回来
2. VC1模态到VC2并dismiss回来
希望通过这段打印结果, 会对ViewController的生命周期有一个自己的认识和理解.