ViewController生命周期

这里只是对几个主要的VC的生命周期函数做一下分析:
1.在A控制器中实现以下函数

- (void)viewDidLoad {
    [super viewDidLoad];
    NSLog(@"A-viewDidLoad");

    UIButton *A = [[UIButton alloc] initWithFrame:CGRectMake(100, 100, 50, 50)];
    A.backgroundColor = [UIColor blueColor];
    [self.view addSubview:A];
    [A addTarget:self action:@selector(go) forControlEvents:UIControlEventTouchUpInside];
}

- (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];
    NSLog(@"A-viewWillAppear");
}

- (void)viewDidAppear:(BOOL)animated {
    [super viewDidAppear:animated];
    NSLog(@"A-viewDidAppear");
}

- (void)viewWillDisappear:(BOOL)animated {
    [super viewWillDisappear:animated];
    NSLog(@"A-viewWillDisappear");
}

- (void)viewDidDisappear:(BOOL)animated {
    [super viewDidDisappear:animated];
    NSLog(@"A-viewDidDisappear");
}

- (void)go {
    B *b = [MaskViewController new];
    [self.navigationController pushViewController:b animated:YES];
    //    [self presentViewController:b animated:YES completion:nil];
}

2.同样在B控制器中实现以下函数

- (void)viewDidLoad {
    [super viewDidLoad];
    NSLog(@"B-viewDidLoad");
    //设置一个背景色,方便查看界面切换效果
    self.view.backgroundColor = [UIColor blueColor];

    UIButton *B = [[UIButton alloc] initWithFrame:CGRectMake(100, 100, 50, 50)];
    B.backgroundColor = [UIColor blueColor];
    [self.view addSubview:B];
    [B addTarget:self action:@selector(back) forControlEvents:UIControlEventTouchUpInside];
}

- (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];
    NSLog(@"B-viewWillAppear");
}

- (void)viewDidAppear:(BOOL)animated {
    [super viewDidAppear:animated];
    NSLog(@"B-viewDidAppear");
}

- (void)viewWillDisappear:(BOOL)animated {
    [super viewWillDisappear:animated];
    NSLog(@"B-viewWillDisappear");
}

- (void)viewDidDisappear:(BOOL)animated {
    [super viewDidDisappear:animated];
    NSLog(@"B-viewDidDisappear");
}

- (void)back {
[self.navigationController popViewControllerAnimated:YES];
//    [self dismissViewControllerAnimated:YES completion:nil];
}

3.首先,我们使用Navigation的push和pop进行测验:
打印结果如下
运行程序:

 A-viewDidLoad
 A-viewWillAppear
 A-viewDidAppear

点击A按钮:

以下是点击按钮后,界面B没加载完全时调用
 A-viewWillDisappear
 B-viewDidLoad
 B-viewWillAppear
以下是界面B完全覆盖界面时调用
 A-viewDidDisappear
 B-viewDidAppear

点击B按钮:

以下是点击按钮后,界面A没完全加载出来时调用
 B-viewWillDisappear
 A-viewWillAppear
以下是界面A完全覆盖界面时调用
 B-viewDidDisappear
 A-viewDidAppear

4.接下来我们”封印”push和pop跳转,“解封”模态跳转:
打印结果如下
运行程序:

 A-viewDidLoad
 A-viewWillAppear
 A-viewDidAppear

点击A按钮:

以下是点击按钮后,界面B没加载完全时调用
 **B-viewDidLoad
 A-viewWillDisappear**
 B-viewWillAppear
以下是界面B完全覆盖界面时调用
 **B-viewDidAppear
 A-viewDidDisappear**

点击B按钮:

以下是点击按钮后,界面A没完全加载出来时调用
 B-viewWillDisappear
 A-viewWillAppear
以下是界面A完全覆盖界面时调用
 **A-viewDidAppear
 B-viewDidDisappear**

5.发现什么不同了吗,不错,不同地方我已经都加粗了。从打印的结果我们可以看出,两种跳转的VC,除了显示等不同外,生命周期也是不同的,在这些“生命周期函数”中执行一些代码的时候,一定要注意他们的执行顺序,不然很多隐形的BUG都会出现。举个例子:在调整界面的statusBar的样式时,界面跳来跳去的同时,我们就要根据界面的背景调整statusBar的样式,而调整的语句放在这些生命周期函数中是再好不过了,所以要时刻注意它们的调用顺序,以防出现改变样式的代码执行了,界面却不发生变化,不要以为模拟器抽风了,其实是在后面,有一个“生命周期函数”,偷偷的把样式改回去了。

你可能感兴趣的:(IOS)