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.

释放接收器占用的内存。

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

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 202,607评论 5 476
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 85,047评论 2 379
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 149,496评论 0 335
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 54,405评论 1 273
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 63,400评论 5 364
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 48,479评论 1 281
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 37,883评论 3 395
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 36,535评论 0 256
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 40,743评论 1 295
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 35,544评论 2 319
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 37,612评论 1 329
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 33,309评论 4 318
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 38,881评论 3 306
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 29,891评论 0 19
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 31,136评论 1 259
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 42,783评论 2 349
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 42,316评论 2 342

推荐阅读更多精彩内容