在iOS開發中UIViewController
扮演者很是重要的角色,它是視圖view
和數據model
的橋樑,經過UIViewController
的管理有條不紊的將數據展現在視圖上。做爲UIKit中最基本的一個類,通常複雜的項目都離不開UIViewController
做爲基類。因此瞭解UIViewController
的整個生命週期是有必要的。bash
咱們先看一下下面有關UIViewController
生命週期有關的幾個函數:app
+ (void)initialize {
NSLog(@"======== 類初始化方法: initialize =======\n");
}
- (instancetype)init {
self = [super init];
NSLog(@"======== 實例初始化方法: init =======\n");
return self;
}
- (instancetype)initWithCoder:(NSCoder *)aDecoder {
self = [super initWithCoder:aDecoder];
NSLog(@"======== 從歸檔初始化: initWithCoder:(NSCoder *)aDecoder =======\n");
return self;
}
- (void)loadView {
[super loadView];
NSLog(@"======== 加載視圖: loadView =======\n");
}
#pragma mark- life cycle
- (void)viewDidLoad {
[super viewDidLoad];
NSLog(@"======== 將要加載視圖: viewDidLoad =======\n");
}
- (void)viewWillLayoutSubviews {
[super viewWillLayoutSubviews];
NSLog(@"======== 將要佈局子視圖: viewWillLayoutSubviews =======\n");
}
- (void)viewDidLayoutSubviews {
[super viewDidLayoutSubviews];
NSLog(@"======== 已經佈局子視圖: viewDidLayoutSubviews =======\n");
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
NSLog(@"======== 收到內存警告: didReceiveMemoryWarning =======\n");
}
- (void)viewWillAppear:(BOOL)animated {
[super viewWillAppear:animated];
NSLog(@"======== 視圖將要出現: viewWillAppear:(BOOL)animated =======\n");
}
- (void)viewDidAppear:(BOOL)animated {
[super viewDidAppear:animated];
NSLog(@"======== 視圖已經出現: viewDidAppear:(BOOL)animated =======\n");
}
- (void)viewWillDisappear:(BOOL)animated {
[super viewWillDisappear:animated];
NSLog(@"======== 視圖將要消失: viewWillDisappear:(BOOL)animated =======\n");
}
- (void)viewDidDisappear:(BOOL)animated {
[super viewDidDisappear:animated];
NSLog(@"======== 視圖已經消失: viewDidDisappear:(BOOL)animated =======\n");
}
- (void)dealloc {
NSLog(@"======== 釋放: dealloc =======\n");
}
複製代碼
看到這麼多的函數是否是感受理解起來很複雜,其實從上往下看一遍以後就能夠了解到他們關係其實很明朗。咱們運行一下看看他們之間的調用順序:函數
2019-05-21 09:29:41.532655+0800 ThinTableVIew1[77845:3751647] ======== ViewController1 類初始化方法: initialize =======
2019-05-21 09:29:41.533321+0800 ThinTableVIew1[77845:3751647] ======== ViewController1 實例初始化方法: init =======
2019-05-21 09:29:41.539746+0800 ThinTableVIew1[77845:3751647] ======== ViewController1 加載視圖: loadView =======
2019-05-21 09:29:41.539975+0800 ThinTableVIew1[77845:3751647] ======== ViewController1 將要加載視圖: viewDidLoad =======
2019-05-21 09:29:41.540280+0800 ThinTableVIew1[77845:3751647] ======== ViewController1 視圖將要出現: viewWillAppear:(BOOL)animated =======
2019-05-21 09:29:41.581539+0800 ThinTableVIew1[77845:3751647] ======== ViewController1 將要佈局子視圖: viewWillLayoutSubviews =======
2019-05-21 09:29:41.581755+0800 ThinTableVIew1[77845:3751647] ======== ViewController1 已經佈局子視圖: viewDidLayoutSubviews =======
2019-05-21 09:29:42.086186+0800 ThinTableVIew1[77845:3751647] ======== ViewController1 視圖已經出現: viewDidAppear:(BOOL)animated =======
2019-05-21 09:30:11.567953+0800 ThinTableVIew1[77845:3751647] ======== ViewController1 視圖將要消失: viewWillDisappear:(BOOL)animated =======
2019-05-21 09:30:11.568210+0800 ThinTableVIew1[77845:3751647] ======== ViewController 視圖將要出現: viewWillAppear:(BOOL)animated =======
2019-05-21 09:30:12.074866+0800 ThinTableVIew1[77845:3751647] ======== ViewController1 視圖已經消失: viewDidDisappear:(BOOL)animated =======
2019-05-21 09:30:12.075103+0800 ThinTableVIew1[77845:3751647] ======== ViewController 視圖已經出現: viewDidAppear:(BOOL)animated =======
2019-05-21 09:30:12.075378+0800 ThinTableVIew1[77845:3751647] ======== ViewController1 釋放: dealloc =======
複製代碼
ViewController
是上層視圖ViewController1
是push過去的二級視圖。咱們能夠看到ViewController1
從初始化到釋放的整個過程。佈局
+ (void)initialize
:函數並不會每次建立對象都調用,只有在第一次初始化的時候纔會調用,再次建立將不會調用initialize
方法。init
方法和initCoder
方法類似,知識被調用的環境不同。若是用代碼初始化,會調用init
方法,從nib文件或者歸檔(xib
、storyboard
)進行初始化會調用initCoder
。initCoder
是NSCoding
協議中的方法,NSCoding
是負責編碼解碼,歸檔處理的協議。loadView
:是開始加載view
的起始方法,除非手動調用,不然在ViewController
的生命週期中只調用一次。viewDidLoad
:是咱們最經常使用的方法,類成員對象和變量的初始化咱們都會放在這個方法中。在建立類後不管視圖展示仍是消失,這個方法也只會在佈局是調用一次。viewWillAppear:(BOOL)animated
:方法 是在視圖將要展示出來的時候調用。viewWillLayoutSubviews
:方法是在將要佈局子視圖的時候調用。viewDidLayoutSubviews
:方法是在子視圖佈局完成後調用。viewDidAppear:(BOOL)animated
:方法是視圖已經出現。viewWillDisappear:(BOOL)animated
:方法是視圖即將消失。viewDidDisappear:(BOOL)animated
:視圖已經消失。dealloc
:ViewController
被釋放時調用。從上面的打印咱們能夠看到ViewController
的整個生命週期,從初始化到被釋放。其中在退出ViewController
控制器的時候能夠看到程序會先調用ViewController
的viewWillDisappear:(BOOL)animated
方法而後會調用上一級視圖的viewWillAppear:(BOOL)animated
方法,而後纔會調用當前視圖的viewDidDisappear:(BOOL)animated
方法。ui