iOS UIViewController生命周期

前言
一直忽略的视图控制器的生命周期问题,当被面试官追问时,发现自己知之甚浅,特此,总结下来。

视图控制器创建方式

方式一(init加载):
CodeViewController *VC = [[CodeViewController alloc]init];

方式二(xib加载):
XibViewController *VC = [[XibViewController alloc]initWithNibName:@"XibViewController" bundle:[NSBundle mainBundle]];
 
方式三(Storyboard加载):
UIStoryboard *sb = [UIStoryboard storyboardWithName:@"StoryBoardVC" bundle:[NSBundle mainBundle]];
StbViewController *VC = [sb instantiateViewControllerWithIdentifier:@"StoryBoardViewController"];

方式四(NSCoding协议加载):
NSCoderViewController  *VC = [[NSCoderViewController alloc]initWithCoder:nil];

[self presentViewController:VC animated:YES completion:nil];

方式一输出:

2019-01-18 17:27:08.801819+0800 LifeCyleVC[6591:1028822] +[CodeViewController initialize]
2019-01-18 17:27:08.802087+0800 LifeCyleVC[6591:1028822] -[CodeViewController initWithNibName:bundle:]
2019-01-18 17:27:08.802268+0800 LifeCyleVC[6591:1028822] -[CodeViewController init]
2019-01-18 17:27:08.802917+0800 LifeCyleVC[6591:1028822] -[CodeViewController loadView]
2019-01-18 17:27:08.803134+0800 LifeCyleVC[6591:1028822] -[CodeViewController viewDidLoad]
2019-01-18 17:27:08.804736+0800 LifeCyleVC[6591:1028822] -[CodeViewController viewWillAppear:]
2019-01-18 17:27:08.806116+0800 LifeCyleVC[6591:1028822] -[CodeViewController viewWillLayoutSubviews]
2019-01-18 17:27:08.806240+0800 LifeCyleVC[6591:1028822] -[CodeViewController viewDidLayoutSubviews]
2019-01-18 17:27:09.307427+0800 LifeCyleVC[6591:1028822] -[CodeViewController viewDidAppear:]

方式二输出:

2019-01-18 16:32:07.414444+0800 LifeCyleVC[5502:834637] +[XibViewController initialize]
2019-01-18 16:32:07.414708+0800 LifeCyleVC[5502:834637] -[XibViewController initWithNibName:bundle:]
2019-01-18 16:32:07.416775+0800 LifeCyleVC[5502:834637] -[XibViewController loadView]
2019-01-18 16:32:07.416925+0800 LifeCyleVC[5502:834637] -[XibViewController viewDidLoad]
2019-01-18 16:32:07.418489+0800 LifeCyleVC[5502:834637] -[XibViewController viewWillAppear:]
2019-01-18 16:32:07.420077+0800 LifeCyleVC[5502:834637] -[XibViewController viewWillLayoutSubviews]
2019-01-18 16:32:07.420202+0800 LifeCyleVC[5502:834637] -[XibViewController viewDidLayoutSubviews]
2019-01-18 16:32:07.922041+0800 LifeCyleVC[5502:834637] -[XibViewController viewDidAppear:]

方式三输出:

2019-01-21 18:35:39.242503+0800 LifeCyleVC[50009:3700866] +[NextViewController initialize]
2019-01-21 18:35:39.246208+0800 LifeCyleVC[50009:3700866] -[NextViewController initWithCoder:]
2019-01-21 18:35:39.246431+0800 LifeCyleVC[50009:3700866] -[NextViewController awakeFromNib]
2019-01-21 18:35:39.256965+0800 LifeCyleVC[50009:3700866] -[NextViewController loadView]
2019-01-21 18:35:39.257161+0800 LifeCyleVC[50009:3700866] -[NextViewController viewDidLoad]
2019-01-21 18:35:39.259448+0800 LifeCyleVC[50009:3700866] -[NextViewController viewWillAppear:]
2019-01-21 18:35:39.266274+0800 LifeCyleVC[50009:3700866] -[NextViewController viewWillLayoutSubviews]
2019-01-21 18:35:39.266608+0800 LifeCyleVC[50009:3700866] -[NextViewController viewDidLayoutSubviews]
2019-01-21 18:35:39.778369+0800 LifeCyleVC[50009:3700866] -[NextViewController viewDidAppear:]

方式四输出:

2019-01-18 17:19:47.437557+0800 LifeCyleVC[6466:1012816] +[NSCoderViewController initialize]
2019-01-18 17:19:47.437860+0800 LifeCyleVC[6466:1012816] -[NSCoderViewController initWithCoder:]
2019-01-18 17:19:47.438482+0800 LifeCyleVC[6466:1012816] -[NSCoderViewController loadView]
2019-01-18 17:19:47.438682+0800 LifeCyleVC[6466:1012816] -[NSCoderViewController viewDidLoad]
2019-01-18 17:19:47.440091+0800 LifeCyleVC[6466:1012816] -[NSCoderViewController viewWillAppear:]
2019-01-18 17:19:47.441523+0800 LifeCyleVC[6466:1012816] -[NSCoderViewController viewWillLayoutSubviews]
2019-01-18 17:19:47.441673+0800 LifeCyleVC[6466:1012816] -[NSCoderViewController viewDidLayoutSubviews]
2019-01-18 17:19:47.441743+0800 LifeCyleVC[6466:1012816] -[ViewController viewWillLayoutSubviews]
2019-01-18 17:19:47.441821+0800 LifeCyleVC[6466:1012816] -[ViewController viewDidLayoutSubviews]
2019-01-18 17:19:47.942351+0800 LifeCyleVC[6466:1012816] -[NSCoderViewController viewDidAppear:]

视图销毁调用方法:

2019-01-21 17:33:44.561425+0800 LifeCyleVC[49067:3464394] -[ViewController viewWillDisappear:]
2019-01-21 17:33:44.569643+0800 LifeCyleVC[49067:3464394] -[ViewController viewWillLayoutSubviews]
2019-01-21 17:33:44.569801+0800 LifeCyleVC[49067:3464394] -[ViewController viewDidLayoutSubviews]
2019-01-21 17:33:45.073069+0800 LifeCyleVC[49067:3464394] -[ViewController viewDidDisappear:]
2019-01-21 17:33:45.073069+0800 LifeCyleVC[49067:3464394] -[ViewController dealloc]

+(void)load

https://developer.apple.com/documentation/objectivec/nsobject/1418815-load?language=objc

Invoked whenever a class or category is added to the Objective-C runtime; implement this method to perform class-specific behavior upon loading.

当一个类或者类别被加载到OC运行时时调用,每个类、类别的+load方法,在程序运行时只会加载一次

  • 先加载父类的+load方法,再加载子类的+load方法,不同的类的+load方法调用和编译时添加的顺序一致(Compile Sources)
  • 先加载类的+load方法(包括子类,也就是说子类的+load方法在类别的+load方法调用之前调用),再加载类别的+load方法,不同的类别的+load方法调用和编译时添加的顺序一致(Compile Sources)
  • 子类的+load方法未实现并不会调用父类的+load方法

概括来说:类-->子类-->类别

+(void)initialize

https://developer.apple.com/documentation/objectivec/nsobject/1418639-initialize?language=occ

Initializes the class before it receives its first message.

初始化类,在类第一次收到消息之前调用

  • 先调用父类的initialize,再调用子类的initialize方法
  • runtime发送initialize消息是线程安全的,因为initialize是在类发送第一条消息之前调用,并且任何线程都必须在完成initialize方法之后再向类发送消息。
  • 若是子类没有实现initialize方法,则会调用父类initialize方法,所以一个父类的initialize可能被多次调用,子类实现initialize方法时,会覆盖父类initialize方法.
  • 当有多个Category都实现了initialize方法,会覆盖类中的方法,只执行一个(会执行Compile Sources 列表中最后一个Category 的initialize方法)

- (instancetype)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil

https://developer.apple.com/documentation/uikit/uiviewcontroller/1621359-initwithnibname?language=occ

Returns a newly initialized view controller with the nib file in the specified bundle.

从指定的nib文件中初始化视图控制器。

- (instancetype)init

https://developer.apple.com/documentation/objectivec/nsobject/1418641-init?language=occ

Implemented by subclasses to initialize a new object (the receiver) immediately after memory for it has been allocated.

由子类实现初始化一个新的对象(接收者)后立即内存分配

- (instancetype)initWithCoder:(NSCoder *)aDecoder

当从nib或者storyboard中初始化对象时,则会调用该方法。可以用该方法来重新定义我们在nib中已经设置的各项属性。

  • 遵循NSCoding协议的对象调用才能调用该方法进行初始化对象。

- (void)awakeFromNib

https://developer.apple.com/documentation/objectivec/nsobject/1402907-awakefromnib?language=occ

Prepares the receiver for service after it has been loaded from an Interface Builder archive, or nib file.

当从nib文件或者Interface Builder archive中加载视图调用

  • 在初始化的过程中,每个对象都会使用它合适的方法进行初始化。遵循NSCoding协议的对象调用initWithCoder:进行初始化,不遵循NSCoding协议的其他对象调用init方法初始化。
  • 该方法必须在对象被加载和初始化之后调用
  • 当一个对象调用awakeFromNib方法时,要确保所有的outlet和事件关联都被建立。

- (void)loadView

https://developer.apple.com/documentation/uikit/uiviewcontroller/1621454-loadview?language=occ

Creates the view that the controller manages.

创建视图控制器的视图

如果视图控制器关联的是nib文件,那么就从nib文件中加载view,如果没有关联nib文件,则该方法创建一个UIView对象。

- (void)viewDidLoad

https://developer.apple.com/documentation/uikit/uiviewcontroller/1621495-viewdidload?language=occ

Called after the controller's view is loaded into memory.

当一个控制器的视图被加载到内存中时调用

- (void)viewWillAppear:(BOOL)animated

https://developer.apple.com/documentation/uikit/uiviewcontroller/1621510-viewwillappear?language=objc

Notifies the view controller that its view is about to be added to a view hierarchy.

通知视图控制器,视图将要被添加到视图层次结构中。
你可以覆盖这个方法执行自定义任务与视图显示相关,例如你可以在此方法中改变状态栏的风格。

- (void)viewWillLayoutSubviews

https://developer.apple.com/documentation/uikit/uiviewcontroller/1621437-viewwilllayoutsubviews?language=occ

Called to notify the view controller that its view is about to layout its subviews.

通知视图控制器将要布局它的子视图时调用。

- (void)viewDidLayoutSubviews

https://developer.apple.com/documentation/uikit/uiviewcontroller/1621398-viewdidlayoutsubviews?language=occ

Called to notify the view controller that its view has just laid out its subviews.

通知视图控制器刚刚布局好子视图时调用。

- (void)viewDidAppear:(BOOL)animated

https://developer.apple.com/documentation/uikit/uiviewcontroller/1621423-viewdidappear?language=objc

Notifies the view controller that its view was added to a view hierarchy.

通知视图控制器它的视图已经加到视图层次结构中了。

- (void)viewWillDisappear:(BOOL)animated

https://developer.apple.com/documentation/uikit/uiviewcontroller/1621485-viewwilldisappear?language=objc

Notifies the view controller that its view is about to be removed from a view hierarchy.

通知视图控制器视图将要从它的视图层次结构中移除。

- (void)viewDidDisappear:(BOOL)animated

https://developer.apple.com/documentation/uikit/uiviewcontroller/1621477-viewdiddisappear?language=occ

Notifies the view controller that its view was removed from a view hierarchy.

通知视图控制器视图已经从它的视图层次结构中移除。

- (void)dealloc

https://developer.apple.com/documentation/objectivec/nsobject/1571947-dealloc?language=occ

Deallocates the memory occupied by the receiver.

释放接收器占用的内存。

结语
由于笔者能力有限,若文章中有什么错误或者需要补充的部分,希望大家不吝赐教,非常感谢。

推荐阅读更多精彩内容