这篇主要为了记录一下UIViewController各个方法调用的顺序,以及pushViewController和presentViewController之后的UIViewController中各个方法的加载顺序
开始运行,首先加载的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<UITouch *> *)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<UITouch *> *)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]