CodeInitViewController.m
// // CodeInitViewController.m // ControllerLifeCycle // // Created by Ternence on 2021/4/29. // #import "CodeInitViewController.h" #import "XibInitViewController.h" @interface CodeInitViewController () @end @implementation CodeInitViewController + (void)load { [super load]; NSLog(@"CodeVC------%s", __func__); } + (void)initialize { [super initialize]; NSLog(@"CodeVC------%s", __func__); } + (instancetype)alloc { NSLog(@"CodeVC------%s", __func__); return [super alloc]; } - (nullable instancetype)initWithCoder:(NSCoder *)coder { NSLog(@"CodeVC------%s", __func__); return [super initWithCoder:coder]; } - (instancetype)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil { NSLog(@"CodeVC------%s", __func__); return [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil]; } - (instancetype)init { NSLog(@"CodeVC------%s", __func__); return [super init]; } - (void)loadView { [super loadView]; NSLog(@"CodeVC------%s", __func__); } - (void)viewWillAppear:(BOOL)animated { [super viewWillAppear:animated]; NSLog(@"CodeVC------%s", __func__); } - (void)viewDidAppear:(BOOL)animated { [super viewDidAppear:animated]; NSLog(@"CodeVC------%s", __func__); } - (void)viewWillDisappear:(BOOL)animated { [super viewWillDisappear:animated]; NSLog(@"CodeVC------%s", __func__); } - (void)viewDidDisappear:(BOOL)animated { [super viewDidDisappear:animated]; NSLog(@"CodeVC------%s", __func__); } - (void)viewDidLoad { [super viewDidLoad]; NSLog(@"CodeVC------%s", __func__); self.view.backgroundColor = [UIColor redColor]; } - (void)viewWillLayoutSubviews { [super viewWillLayoutSubviews]; NSLog(@"CodeVC------%s", __func__); } - (void)viewDidLayoutSubviews { [super viewDidLayoutSubviews]; NSLog(@"CodeVC------%s", __func__); } - (void)didReceiveMemoryWarning { [super didReceiveMemoryWarning]; NSLog(@"CodeVC------%s", __func__); } - (void)dealloc { NSLog(@"CodeVC------%s", __func__); } - (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event { [super touchesBegan:touches withEvent:event]; XibInitViewController *vc = [[ XibInitViewController alloc]init]; [self.navigationController pushViewController:vc animated:YES]; } @end
二. 方法说明
load 程序启动后,在main函数调用之前,系统会加载所有load方法,提前进行一些资源包配置或hook initialize 当前类或其子类未被初始化过时会首次调用,若以后当前类或其子类再次初始化不会再调用,一般提前为初始化做一些工作 alloc 系统为当前类分配内存时调用,在C语言中就是malloc这一步 initWithCoder 通过storyboard方式实例化的vc,需要经过反序列化,这个方法会被调用,其他不会调用 initWithNibName:bundle: 通过Xib或init方法实例化的vc,这个方法都会被调用,其实init方法最终都会走该方法 init 通过纯代码实例化的vc会调用,最终会走initWithNibName:bundle loadView 实例化vc后,可以加载一些系统常规view viewDidLoad 一般加载自定义view或初始化熟悉,视图加载完毕后会调用 viewWillDisappear: 视图即将出现调用 viewDidAppear: 视图已经出现会调用 viewWillDisappear: 视图即将消失调用 viewDidDisappear: 视图已经消失调用 viewWillLayoutSubviews 视图加载完毕后将要布局 viewDidLayoutSubviews 视图加载完毕后布局也完成了 didReceiveMemoryWarning 加载视图时,内存消耗太大,出现内存警告会调用 dealloc 实例化被销毁,运行内存的回收会调用
三. Debug log
- 程序启动,加载storyboard实例化
BoardInitViewController
2021-04-29 18:33:07.317496+0800 ControllerLifeCycle[6270:5044336] XibVC------+[XibInitViewController load] 2021-04-29 18:33:07.319472+0800 ControllerLifeCycle[6270:5044336] boardVC------+[BoardInitViewController load] 2021-04-29 18:33:07.319605+0800 ControllerLifeCycle[6270:5044336] CodeVC------+[CodeInitViewController load] 2021-04-29 18:33:07.369674+0800 ControllerLifeCycle[6270:5044336] boardVC------+[BoardInitViewController initialize] 2021-04-29 18:33:07.369789+0800 ControllerLifeCycle[6270:5044336] boardVC------+[BoardInitViewController alloc] 2021-04-29 18:33:07.369918+0800 ControllerLifeCycle[6270:5044336] boardVC-------[BoardInitViewController initWithCoder:] 2021-04-29 18:33:07.410110+0800 ControllerLifeCycle[6270:5044336] boardVC-------[BoardInitViewController loadView] 2021-04-29 18:33:07.410246+0800 ControllerLifeCycle[6270:5044336] boardVC-------[BoardInitViewController viewDidLoad] 2021-04-29 18:33:07.410867+0800 ControllerLifeCycle[6270:5044336] boardVC-------[BoardInitViewController viewWillAppear:] 2021-04-29 18:33:07.412541+0800 ControllerLifeCycle[6270:5044336] boardVC-------[BoardInitViewController viewWillLayoutSubviews] 2021-04-29 18:33:07.412663+0800 ControllerLifeCycle[6270:5044336] boardVC-------[BoardInitViewController viewDidLayoutSubviews] 2021-04-29 18:33:07.418158+0800 ControllerLifeCycle[6270:5044336] boardVC-------[BoardInitViewController viewDidAppear:]
- 从
BoardInitViewController
中push到纯代码实例化的CodeInitViewController
中
2021-04-29 18:34:45.194133+0800 ControllerLifeCycle[6270:5044336] CodeVC------+[CodeInitViewController initialize] 2021-04-29 18:34:45.194269+0800 ControllerLifeCycle[6270:5044336] CodeVC------+[CodeInitViewController alloc] 2021-04-29 18:34:45.194399+0800 ControllerLifeCycle[6270:5044336] CodeVC-------[CodeInitViewController init] 2021-04-29 18:34:45.194518+0800 ControllerLifeCycle[6270:5044336] CodeVC-------[CodeInitViewController initWithNibName:bundle:] 2021-04-29 18:34:45.200030+0800 ControllerLifeCycle[6270:5044336] CodeVC-------[CodeInitViewController loadView] 2021-04-29 18:34:45.200188+0800 ControllerLifeCycle[6270:5044336] CodeVC-------[CodeInitViewController viewDidLoad] 2021-04-29 18:34:45.200374+0800 ControllerLifeCycle[6270:5044336] boardVC-------[BoardInitViewController viewWillDisappear:] 2021-04-29 18:34:45.200515+0800 ControllerLifeCycle[6270:5044336] CodeVC-------[CodeInitViewController viewWillAppear:] 2021-04-29 18:34:45.220143+0800 ControllerLifeCycle[6270:5044336] CodeVC-------[CodeInitViewController viewWillLayoutSubviews] 2021-04-29 18:34:45.220331+0800 ControllerLifeCycle[6270:5044336] CodeVC-------[CodeInitViewController viewDidLayoutSubviews] 2021-04-29 18:34:45.744908+0800 ControllerLifeCycle[6270:5044336] boardVC-------[BoardInitViewController viewDidDisappear:] 2021-04-29 18:34:45.745100+0800 ControllerLifeCycle[6270:5044336] CodeVC-------[CodeInitViewController viewDidAppear:]
- 从
CodeInitViewController
中push到Xib实例化的XibInitViewController
中
2021-04-29 18:35:29.448921+0800 ControllerLifeCycle[6270:5044336] XibVC------+[XibInitViewController initialize] 2021-04-29 18:35:29.449069+0800 ControllerLifeCycle[6270:5044336] XibVC------+[XibInitViewController alloc] 2021-04-29 18:35:29.449190+0800 ControllerLifeCycle[6270:5044336] XibVC-------[XibInitViewController init] 2021-04-29 18:35:29.449288+0800 ControllerLifeCycle[6270:5044336] XibVC-------[XibInitViewController initWithNibName:bundle:] 2021-04-29 18:35:29.450580+0800 ControllerLifeCycle[6270:5044336] XibVC-------[XibInitViewController loadView] 2021-04-29 18:35:29.450757+0800 ControllerLifeCycle[6270:5044336] XibVC-------[XibInitViewController viewDidLoad] 2021-04-29 18:35:29.450920+0800 ControllerLifeCycle[6270:5044336] CodeVC-------[CodeInitViewController viewWillDisappear:] 2021-04-29 18:35:29.451058+0800 ControllerLifeCycle[6270:5044336] XibVC-------[XibInitViewController viewWillAppear:] 2021-04-29 18:35:29.460843+0800 ControllerLifeCycle[6270:5044336] XibVC-------[XibInitViewController viewWillLayoutSubviews] 2021-04-29 18:35:29.460981+0800 ControllerLifeCycle[6270:5044336] XibVC-------[XibInitViewController viewDidLayoutSubviews] 2021-04-29 18:35:29.963047+0800 ControllerLifeCycle[6270:5044336] CodeVC-------[CodeInitViewController viewDidDisappear:] 2021-04-29 18:35:29.963345+0800 ControllerLifeCycle[6270:5044336] XibVC-------[XibInitViewController viewDidAppear:]
- 将
XibInitViewController
Pop到CodeInitViewController
2021-04-29 18:36:28.460722+0800 ControllerLifeCycle[6270:5044336] XibVC-------[XibInitViewController viewWillDisappear:] 2021-04-29 18:36:28.460881+0800 ControllerLifeCycle[6270:5044336] CodeVC-------[CodeInitViewController viewWillAppear:] 2021-04-29 18:36:28.971655+0800 ControllerLifeCycle[6270:5044336] XibVC-------[XibInitViewController viewDidDisappear:] 2021-04-29 18:36:28.971818+0800 ControllerLifeCycle[6270:5044336] CodeVC-------[CodeInitViewController viewDidAppear:] 2021-04-29 18:36:28.972008+0800 ControllerLifeCycle[6270:5044336] XibVC-------[XibInitViewController dealloc]
- 将
CodeInitViewController
Pop到BoardInitViewController
2021-04-29 18:37:09.095856+0800 ControllerLifeCycle[6270:5044336] CodeVC-------[CodeInitViewController viewWillDisappear:] 2021-04-29 18:37:09.095988+0800 ControllerLifeCycle[6270:5044336] boardVC-------[BoardInitViewController viewWillAppear:] 2021-04-29 18:37:09.599896+0800 ControllerLifeCycle[6270:5044336] CodeVC-------[CodeInitViewController viewDidDisappear:] 2021-04-29 18:37:09.600184+0800 ControllerLifeCycle[6270:5044336] boardVC-------[BoardInitViewController viewDidAppear:] 2021-04-29 18:37:09.600453+0800 ControllerLifeCycle[6270:5044336] CodeVC-------[CodeInitViewController dealloc]
四. 总结
main
函数加载前,先加载所有controller的+load方法,其中顺序是:Xib
->Storyboard
->Code
- 控制器初始化流程是:初始化A->加载视图A->将要显示A->将要布局A->布局A完成->已经显示A
- 控制器A Push B生命流程:初始化B->加载B->A将要消失->B将要显示->B将要布局->B已经布局->A已经消失->B已经显示
- 只有通过storyboard创建的vc才会调用
initWithCoder
,不会调用initWithNibName:bundle:
- 调用了
init
方法的vc(xib或纯代码),最终会调用initWithNibName:bundle: