前言:前段时间面试iOS ,问到了UIViewController的生命周期,顺序什么的我也都知道,但是回答的就是不好,顺带着记录一下。
- 开发过程中,常用的几个方法如下
+ (void)load;
- (void)loadView;
- (void)viewDidLoad;
- (void)viewWillAppear:(BOOL)animated;
- (void)viewDidAppear:(BOOL)animated;
- (void)viewWillDisappear:(BOOL)animated;
- (void)viewDidDisappear:(BOOL)animated;
- (void)viewWillLayoutSubviews;
- (void)viewDidLayoutSubviews;
- (void)didReceiveMemoryWarning;
- (void)dealloc;
- 在Demo里面在TestViewController里面创建一个button弹出Test2ViewController,在Test2ViewController里面重写上面的方法, 使用 NSLog(@"%s", func);用来显示当前类名
- (void)viewWillAppear:(BOOL)animated {
[super viewWillAppear:animated];
NSLog(@"%s", __func__);
}
- 运行Demo显示如下
2018-06-20 10:05:09.911273+0800 OpenGLDemo[34448:1057378] +[Test2ViewController load]
(void *) $0 = 0x00006000001f9c00
2018-06-20 10:05:10.549604+0800 OpenGLDemo[34448:1057378] +[CATransaction synchronize] called within transaction
2018-06-20 10:05:10.747135+0800 OpenGLDemo[34448:1057378] INFO: Reveal Server started (Protocol Version 32).
2018-06-20 10:05:23.574440+0800 OpenGLDemo[34448:1057378] 点击了button
2018-06-20 10:05:23.576965+0800 OpenGLDemo[34448:1057378] -[Test2ViewController loadView]
2018-06-20 10:05:23.577123+0800 OpenGLDemo[34448:1057378] -[Test2ViewController viewDidLoad]
2018-06-20 10:05:23.577379+0800 OpenGLDemo[34448:1057378] -[Test2ViewController viewWillAppear:]
2018-06-20 10:05:23.602620+0800 OpenGLDemo[34448:1057378] -[Test2ViewController viewWillLayoutSubviews]
2018-06-20 10:05:23.602749+0800 OpenGLDemo[34448:1057378] -[Test2ViewController viewDidLayoutSubviews]
2018-06-20 10:05:24.106126+0800 OpenGLDemo[34448:1057378] -[Test2ViewController viewDidAppear:]
- 我们可以看到先调用的是+ (void)load 这个方法,虽然是在Test2ViewController里面写的。
然后点击button,push进入Test2ViewController,具体调用顺序
- loadView
- viewDidLoad
- viewWillAppear
- viewWillLayoutSubviews
- viewDidLayoutSubviews
- viewDidAppear
- 然后点击导航上面的black按键,返回到TestViewController 可以看到 调用顺序
2018-06-20 10:11:08.155785+0800 OpenGLDemo[34448:1057378] -[Test2ViewController viewWillDisappear:]
2018-06-20 10:11:08.660287+0800 OpenGLDemo[34448:1057378] -[Test2ViewController viewDidDisappear:]
2018-06-20 10:11:08.660698+0800 OpenGLDemo[34448:1057378] -[Test2ViewController dealloc]
- viewWillDisappear
- viewDidDisappear
- dealloc
- 最后,还有一个- (void)didReceiveMemoryWarning;方法没有调用,此方法主要是内存不够时调用,后续可以模拟下。
网友评论