UIViewController的生命周期比較簡(jiǎn)單,整個(gè)流程調(diào)用代碼順序如下:
initWithNibName:bundle: 初始化UIViewController痹束,執(zhí)行關(guān)鍵數(shù)據(jù)初始化操作张症,非StoryBoard創(chuàng)建UIViewController都會(huì)調(diào)用這個(gè)方法.
initWithCoder: 如果通過(guò)StoryBoard進(jìn)行視圖管理仓技,程序不會(huì)直接初始化一個(gè)UIViewController,StoryBoard會(huì)自動(dòng)初始化或在segue被觸發(fā)時(shí)自動(dòng)初始化吠冤,因此方法initWithNibName:bundle不會(huì)被調(diào)用浑彰,但是initWithCoder會(huì)被調(diào)用。
loadView :當(dāng)訪問(wèn)UIViewController的View屬性時(shí)拯辙,View如果此時(shí)為nil,那么ViewController會(huì)自動(dòng)調(diào)用loadView方法來(lái)初始化一個(gè)UIView并賦值給UIViewController的View郭变;如果沒(méi)有重載lodaView方法,則UIViewController會(huì)從nib或StoryBoard中查找默認(rèn)的loadView涯保,默認(rèn)的loadView會(huì)返回一個(gè)空白的UIView對(duì)象.
viewDidLoad:當(dāng)loadView將view載入內(nèi)存中诉濒,可以對(duì)加載網(wǎng)絡(luò)數(shù)據(jù),視圖的UI初始化.
viewWillAppear:系統(tǒng)在載入所有的數(shù)據(jù)后夕春,在視圖展示之前還可以進(jìn)行進(jìn)一步的調(diào)整(比如設(shè)置狀態(tài)欄方向).
viewWillLayoutSubviews:view 即將布局其Subviews.
viewDidLayoutSubviews:view已經(jīng)布局其Subviews.
viewDidAppear:視圖完全展示在界面之前最后的調(diào)整.
viewWillDisappear:在視圖切換是未荒,當(dāng)前視圖在即將被移除、或被覆蓋是及志,此時(shí)還沒(méi)有調(diào)用removeFromSuperview片排。
viewDidDisappear:view已經(jīng)消失或被覆蓋寨腔,此時(shí)已經(jīng)調(diào)用removeFromSuperView;
dealloc:視圖被銷(xiāo)毀.
didReceiveMemoryWarning:在內(nèi)存足夠的情況下,app的視圖通常會(huì)一直保存在內(nèi)存中率寡,但是如果內(nèi)存不夠迫卢,一些沒(méi)有正在顯示的viewController就會(huì)收到內(nèi)存不夠的警告,然后就會(huì)釋放自己擁有的視圖冶共,以達(dá)到釋放內(nèi)存的目的乾蛤。但是系統(tǒng)只會(huì)釋放內(nèi)存,并不會(huì)釋放對(duì)象的所有權(quán)捅僵,所以通常我們需要在這里將不需要顯示在內(nèi)存中保留的對(duì)象釋放它的所有權(quán)家卖,將其指針置nil。
- (instancetype)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil {
if (self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil]) {
NSLog(@"FlyElephant---%s", __FUNCTION__);
}
return self;
}
- (instancetype)initWithCoder:(NSCoder *)aDecoder {
NSLog(@"FlyElephant---%s", __FUNCTION__);
if (self = [super initWithCoder:aDecoder]) {
NSLog(@"FlyElephant---%s", __FUNCTION__);
}
return self;
}
- (void)loadView {
[super loadView];
NSLog(@"FlyElephant---%s", __FUNCTION__);
self.view = [[UIView alloc] initWithFrame:[UIScreen mainScreen].bounds];
self.view.backgroundColor = [UIColor greenColor];
}
- (void)viewDidLoad {
[super viewDidLoad];
NSLog(@"FlyElephant--%s", __FUNCTION__);
}
- (void)viewWillAppear:(BOOL)animated {
[super viewWillAppear:animated];
NSLog(@"FlyElephant--%s", __FUNCTION__);
}
- (void)viewWillLayoutSubviews {
[super viewWillLayoutSubviews];
NSLog(@"FlyElephant--%s", __FUNCTION__);
}
- (void)viewDidLayoutSubviews {
[super viewDidLayoutSubviews];
NSLog(@"FlyElephant--%s", __FUNCTION__);
}
- (void)viewDidAppear:(BOOL)animated {
NSLog(@"%s", __FUNCTION__);
[super viewDidAppear:animated];
}
- (void)viewWillDisappear:(BOOL)animated {
[super viewWillDisappear:animated];
NSLog(@"FlyElephant--%s", __FUNCTION__);
}
- (void)viewDidDisappear:(BOOL)animated {
[super viewDidDisappear:animated];
NSLog(@"FlyElephant--%s", __FUNCTION__);
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
NSLog(@"FlyElephant--%s", __FUNCTION__);
}
- (void)dealloc {
NSLog(@"%s", __FUNCTION__);
}
非StoryBoard中控制器加載過(guò)程:
StoryBoard中控制器加載過(guò)程: