简述UIViewController的生命周期
[toc]
第一步:初始化
init(这里包括两个函数):
- initWithNibName 非storyBoard(xib或非xib)都走这个方法
- initWithCoder 如果连接使用了storyBoard 走这个方法
awakeFromNib
当awakeFromNib方法被调用时,所有视图的outlet和action已经连接,但还没有被确定,这个方法可以算作适合视图控制器的实例化配合一起使用的,因为有些需要根据用户喜好来进行设置的内容,无法存在storyBoard或xib中,所以可以在awakeFromNib方法中被加载进来。
第二步:加载视图
loadView
loadView 方法不应该直接被调用,而是由系统调用。它会加载或创建一个 view 并把它赋值给 UIViewController 的 view 属性。
直接调用容易造成死循环。viewDidLoad
当 loadView 将 View 加载完成,会调用 viewDidLoad 方法,该方法在 UIViewController 初始化过程中只会执行一次。
第三步:视图布局(页面每次出现都会调用)
- viewWillAppear
- viewWillLayoutSubviews
- viewDidLayoutSubviews
- viewDidAppear
第四步:页面消失(通常是 push/pop )
- viewWillDisappear
- viewDidDisappear
第五步:销毁
- dealloc
- didRecieveMemoryWarning
实践结果:
采用 StoryBoard,主要操作是进入界面后点击 Button push 进入界面 B 然后在 pop 回来。
2018-06-26 22:35:03.309759+0800 LifeCircle_UIViewController[1696:83562] -[ViewController_A initWithCoder:]
2018-06-26 22:35:03.315788+0800 LifeCircle_UIViewController[1696:83562] -[ViewController_A awakeFromNib]
2018-06-26 22:35:03.339617+0800 LifeCircle_UIViewController[1696:83562] -[ViewController_A viewDidLoad]
2018-06-26 22:35:03.340237+0800 LifeCircle_UIViewController[1696:83562] -[ViewController_A viewWillAppear:]
2018-06-26 22:35:03.342314+0800 LifeCircle_UIViewController[1696:83562] -[ViewController_A viewWillLayoutSubviews]
2018-06-26 22:35:03.342666+0800 LifeCircle_UIViewController[1696:83562] -[ViewController_A viewDidLayoutSubviews]
2018-06-26 22:35:03.348016+0800 LifeCircle_UIViewController[1696:83562] -[ViewController_A viewDidAppear:]
2018-06-26 22:37:12.349655+0800 LifeCircle_UIViewController[1696:83562] -[ViewController_B initWithCoder:]
2018-06-26 22:37:12.350268+0800 LifeCircle_UIViewController[1696:83562] -[ViewController_B awakeFromNib]
2018-06-26 22:37:12.359895+0800 LifeCircle_UIViewController[1696:83562] -[ViewController_B viewDidLoad]
2018-06-26 22:37:12.360171+0800 LifeCircle_UIViewController[1696:83562] -[ViewController_A viewWillDisappear:]
2018-06-26 22:37:12.360355+0800 LifeCircle_UIViewController[1696:83562] -[ViewController_B viewWillAppear:]
2018-06-26 22:37:12.391055+0800 LifeCircle_UIViewController[1696:83562] -[ViewController_B viewWillLayoutSubviews]
2018-06-26 22:37:12.391247+0800 LifeCircle_UIViewController[1696:83562] -[ViewController_B viewDidLayoutSubviews]
2018-06-26 22:37:12.897245+0800 LifeCircle_UIViewController[1696:83562] -[ViewController_A viewDidDisappear:]
2018-06-26 22:37:12.897622+0800 LifeCircle_UIViewController[1696:83562] -[ViewController_B viewDidAppear:]
2018-06-26 22:37:17.054357+0800 LifeCircle_UIViewController[1696:83562] -[ViewController_B viewWillDisappear:]
2018-06-26 22:37:17.054547+0800 LifeCircle_UIViewController[1696:83562] -[ViewController_A viewWillAppear:]
2018-06-26 22:37:17.558813+0800 LifeCircle_UIViewController[1696:83562] -[ViewController_B viewDidDisappear:]
2018-06-26 22:37:17.559031+0800 LifeCircle_UIViewController[1696:83562] -[ViewController_A viewDidAppear:]
2018-06-26 22:37:17.559270+0800 LifeCircle_UIViewController[1696:83562] -[ViewController_B dealloc]