UIViewController的生命周期

时间:2021-7-21 作者:qvyue

这篇主要为了记录一下UIViewController各个方法调用的顺序,以及pushViewController和presentViewController之后的UIViewController中各个方法的加载顺序

github中Demo地址

开始运行,首先加载的viewController这个控制器,此时console终端输出的结果是:

2021-02-20 16:48:20.967536+0800 ViewControllerLifyCycle[6870:949942] 方法:-[ViewController loadView]
2021-02-20 16:48:20.976785+0800 ViewControllerLifyCycle[6870:949942] 方法:-[ViewController viewDidLoad]
2021-02-20 16:48:20.976994+0800 ViewControllerLifyCycle[6870:949942] 方法:-[ViewController viewWillAppear:]
2021-02-20 16:48:20.978448+0800 ViewControllerLifyCycle[6870:949942] 方法:-[ViewController viewWillLayoutSubviews]
2021-02-20 16:48:20.978500+0800 ViewControllerLifyCycle[6870:949942] 方法:-[ViewController viewDidLayoutSubviews]
2021-02-20 16:48:21.012287+0800 ViewControllerLifyCycle[6870:949942] 方法:-[ViewController viewDidAppear:]

方法调用的顺序为:

(1)- (void)loadView;
(2)- (void)viewDidLoad;
(3)- (void)viewWillAppear:(BOOL)animated;
(4)- (void)viewWillLayoutSubviews;
(5)- (void)viewDidLayoutSubviews;
(6)- (void)viewDidAppear:(BOOL)animated;

pushViewController

之后,点击屏幕,触发push跳转事件,相应的代码为:

- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
    PushTestViewController *vc = [[PushTestViewController alloc] init];
    [self.navigationController pushViewController:vc animated:YES];
}

之后跳转到pushTestViewController中
console中控制台输出的信息为:

2021-02-20 16:53:40.035062+0800 ViewControllerLifyCycle[6878:952029] 方法:-[PushTestViewController loadView]
2021-02-20 16:53:40.035242+0800 ViewControllerLifyCycle[6878:952029] 方法:-[PushTestViewController viewDidLoad]
2021-02-20 16:53:40.035304+0800 ViewControllerLifyCycle[6878:952029] 方法:-[ViewController viewWillDisappear:]
2021-02-20 16:53:40.035356+0800 ViewControllerLifyCycle[6878:952029] 方法:-[PushTestViewController viewWillAppear:]
2021-02-20 16:53:40.101150+0800 ViewControllerLifyCycle[6878:952029] 方法:-[PushTestViewController viewWillLayoutSubviews]
2021-02-20 16:53:40.101251+0800 ViewControllerLifyCycle[6878:952029] 方法:-[PushTestViewController viewDidLayoutSubviews]
2021-02-20 16:53:40.620949+0800 ViewControllerLifyCycle[6878:952029] 方法:-[ViewController viewDidDisappear:]
2021-02-20 16:53:40.621565+0800 ViewControllerLifyCycle[6878:952029] 方法:-[PushTestViewController viewDidAppear:]

点击后,从viewController push跳转到pushTestViewController中 调用的相关的方法为:

(1)-[pushTestViewController loadView]
(2)-[PushTestViewController viewDidLoad]
(3)-[ViewController viewWillDisappear:]
(4)-[PushTestViewController viewWillAppear:]
(5)-[PushTestViewController viewWillLayoutSubviews]
(6)-[PushTestViewController viewDidLayoutSubviews]
(7)-[ViewController viewDidDisappear:]
(8)-[PushTestViewController viewDidAppear:]

之后,点击返回,从pushTestViewController返回到viewController中,console中的输出信息为:

2021-02-20 16:58:11.642426+0800 ViewControllerLifyCycle[6878:952029] 方法:-[PushTestViewController viewWillDisappear:]
2021-02-20 16:58:11.642607+0800 ViewControllerLifyCycle[6878:952029] 方法:-[ViewController viewWillAppear:]
2021-02-20 16:58:12.158032+0800 ViewControllerLifyCycle[6878:952029] 方法:-[PushTestViewController viewDidDisappear:]
2021-02-20 16:58:12.158573+0800 ViewControllerLifyCycle[6878:952029] 方法:-[ViewController viewDidAppear:]
2021-02-20 16:58:12.159166+0800 ViewControllerLifyCycle[6878:952029] 方法:-[PushTestViewController dealloc]

点击返回,从pushTestViewController返回到viewController中,相应的方法的调用顺序(注意:这个过程并没有调用viewWillLayoutSubviews 和viewDidLayoutSubviews两个方法)

(1)-[PushTestViewController viewWillDisappear:]
(2)-[ViewController viewWillAppear:]
(3)-[PushTestViewController viewDidDisappear:]
(4)-[ViewController viewDidAppear:]
(5)-[PushTestViewController dealloc]

presentViewController

在viewController中点击后,presentViewController到PresentTestViewController中
相应的代码为:

- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
    PresentTestViewController *vc = [[PresentTestViewController alloc] init];
    [self.navigationController presentViewController:vc animated:YES completion:nil];
}

点击后,console终端输出的信息为:

2021-02-20 17:13:06.475383+0800 ViewControllerLifyCycle[6891:958314] 方法:-[PresentTestViewController loadView]
2021-02-20 17:13:06.475564+0800 ViewControllerLifyCycle[6891:958314] 方法:-[PresentTestViewController viewDidLoad]
2021-02-20 17:13:06.483137+0800 ViewControllerLifyCycle[6891:958314] 方法:-[PresentTestViewController viewWillAppear:]
2021-02-20 17:13:06.493596+0800 ViewControllerLifyCycle[6891:958314] 方法:-[PresentTestViewController viewWillLayoutSubviews]
2021-02-20 17:13:06.493676+0800 ViewControllerLifyCycle[6891:958314] 方法:-[PresentTestViewController viewDidLayoutSubviews]
2021-02-20 17:13:06.513509+0800 ViewControllerLifyCycle[6891:958314] 方法:-[PresentTestViewController viewWillLayoutSubviews]
2021-02-20 17:13:06.513613+0800 ViewControllerLifyCycle[6891:958314] 方法:-[PresentTestViewController viewDidLayoutSubviews]
2021-02-20 17:13:07.021259+0800 ViewControllerLifyCycle[6891:958314] 方法:-[PresentTestViewController viewDidAppear:]

从ViewController present到PresentTestViewController中,相应的方法的调用顺序为:(这里没有调用disAppear相关的方法)

(1)-[PresentTestViewController loadView]
(2)-[PresentTestViewController viewDidLoad]
(3)-[PresentTestViewController viewWillAppear:]
(4)-[PresentTestViewController viewWillLayoutSubviews]
(5)-[PresentTestViewController viewDidLayoutSubviews]
(6)-[PresentTestViewController viewWillLayoutSubviews]
(7)-[PresentTestViewController viewDidLayoutSubviews]
(8)-[PresentTestViewController viewDidAppear:]

返回到viewController的话,console输出的相关信息为:

2021-02-20 17:16:19.701213+0800 ViewControllerLifyCycle[6891:958314] 方法:-[PresentTestViewController viewWillDisappear:]
2021-02-20 17:16:20.447432+0800 ViewControllerLifyCycle[6891:958314] 方法:-[PresentTestViewController viewDidDisappear:]
2021-02-20 17:16:20.449339+0800 ViewControllerLifyCycle[6891:958314] 方法:-[PresentTestViewController dealloc]

相应的调用方法的顺序为:

(1)-[PresentTestViewController viewWillDisappear:]
(2)-[PresentTestViewController viewDidDisappear:]
(3)-[PresentTestViewController dealloc]

总结

github中Demo地址

声明:本文内容由互联网用户自发贡献自行上传,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任。如果您发现有涉嫌版权的内容,欢迎发送邮件至:qvyue@qq.com 进行举报,并提供相关证据,工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。