UIViewController的生命周期
前言
UIViewController的生命周期函數(shù):
- init(initWithNibName) 初始化
- loadView 加載自定義的視圖(如果是從xib文件加載, 通常不需要添加此方法; 如果添加該方法, 系統(tǒng)則不會自動創(chuàng)建根view)
- viewDidLoad 視圖加載完成, 可以進行布局,創(chuàng)建其他控件和獲取數(shù)據(jù)
- viewWillAppear 視圖即將出現(xiàn)
- viewDidAppear 視圖已經(jīng)顯示完成
- viewWillDisappear 視圖即將消失
- viewDidDisappear 視圖已經(jīng)消失
- viewWillLayoutSubviews 在view的layoutSubviews方法執(zhí)行前調(diào)用
- viewDidLayoutSubviews 在view的layoutSubviews方法執(zhí)行后調(diào)用
- didReceiveMemoryWarning 系統(tǒng)內(nèi)存警告
- dealloc 銷毀視圖
創(chuàng)建視圖
從xib加載:
init-> viewDidLoad-> viewWillAppear-> viewDidAppear
從自定義視圖加載:
init-> loadView -> viewDidLoad-> viewWillAppear->viewWillLayoutSubviews->(view)layoutSubviews -> viewDidLayoutSubviews -> viewDidAppear
代碼:
//ThirdView.m
@implementation ThirdView
- (void)layoutSubviews {
[super layoutSubviews];
NSLog(@"ThirdView layoutSubviews");
}
@end
//ThirdViewController.m
@implementation ThirdViewController
- (id)init {
if (self = [super init]) {
NSLog(@"ThirdViewController init");
return self;
}
return nil;
}
- (void)viewDidLoad {
[super viewDidLoad];
NSLog(@"ThirdViewController viewDidLoad");
}
- (void)loadView {
NSLog(@"ThirdViewController loadView");
ThirdView *view = [ThirdView new];
self.view = view;
}
- (void)viewWillAppear:(BOOL)animated {
[super viewWillAppear:animated];
NSLog(@"ThirdViewController viewWillAppear");
}
- (void)viewDidAppear:(BOOL)animated {
[super viewDidAppear:animated];
NSLog(@"ThirdViewController viewDidAppear");
}
- (void)viewDidDisappear:(BOOL)animated {
[super viewDidDisappear:animated];
NSLog(@"ThirdViewController viewDidDisappear");
}
- (void)viewWillDisappear:(BOOL)animated {
[super viewWillDisappear:animated];
NSLog(@"ThirdViewController viewWillDisappear");
}
- (void)viewWillLayoutSubviews {
[super viewWillLayoutSubviews];
NSLog(@"ThirdViewController viewWillLayoutSubviews");
}
- (void)viewDidLayoutSubviews {
[super viewDidLayoutSubviews];
NSLog(@"ThirdViewController viewDidLayoutSubviews");
}
@end
輸出:
2018-08-09 20:16:01.577931+0800 LifeCycle[42337:3300297] ThirdViewController init
2018-08-09 20:16:01.581844+0800 LifeCycle[42337:3300297] ThirdViewController loadView
2018-08-09 20:16:01.582142+0800 LifeCycle[42337:3300297] ThirdViewController viewDidLoad
2018-08-09 20:16:01.582462+0800 LifeCycle[42337:3300297] ThirdViewController viewWillAppear
2018-08-09 20:16:01.611263+0800 LifeCycle[42337:3300297] ThirdViewController viewWillLayoutSubviews
2018-08-09 20:16:01.611392+0800 LifeCycle[42337:3300297] ThirdView layoutSubviews
2018-08-09 20:16:01.611473+0800 LifeCycle[42337:3300297] ThirdViewController viewDidLayoutSubviews
2018-08-09 20:16:02.114443+0800 LifeCycle[42337:3300297] ThirdViewController viewDidAppear
視圖顯示邏輯
Controller之間的顯示隱藏邏輯
代碼:
//ViewController.m
- (void)viewWillAppear:(BOOL)animated {
[super viewWillAppear:animated];
NSLog(@"ViewController viewWillAppear");
}
- (void)viewDidAppear:(BOOL)animated {
[super viewDidAppear:animated];
NSLog(@"ViewController viewDidAppear");
}
- (void)viewDidDisappear:(BOOL)animated {
[super viewDidDisappear:animated];
NSLog(@"ViewController viewDidDisappear");
}
- (void)viewWillDisappear:(BOOL)animated {
[super viewWillDisappear:animated];
NSLog(@"ViewController viewWillDisappear");
}
//SecondViewController.m
@implementation SecondViewController
- (void)viewDidLoad {
[super viewDidLoad];
NSLog(@"SecondViewController viewDidLoad");
}
- (void)viewWillAppear:(BOOL)animated {
[super viewWillAppear:animated];
NSLog(@"SecondViewController viewWillAppear");
}
- (void)viewDidAppear:(BOOL)animated {
[super viewDidAppear:animated];
NSLog(@"SecondViewController viewDidAppear");
}
- (void)viewDidDisappear:(BOOL)animated {
[super viewDidDisappear:animated];
NSLog(@"SecondViewController viewDidDisappear");
}
- (void)viewWillDisappear:(BOOL)animated {
[super viewWillDisappear:animated];
NSLog(@"SecondViewController viewWillDisappear");
}
@end
ViewController push SecondViewController輸出:
2018-08-09 20:20:52.184795+0800 LifeCycle[42496:3307472] ViewController viewWillDisappear
2018-08-09 20:20:52.184965+0800 LifeCycle[42496:3307472] SecondViewController viewWillAppear
2018-08-09 20:20:52.711445+0800 LifeCycle[42496:3307472] ViewController viewDidDisappear
2018-08-09 20:20:52.711620+0800 LifeCycle[42496:3307472] SecondViewController viewDidAppear
SecondViewController pop back to ViewController輸出:
2018-08-09 20:23:37.193745+0800 LifeCycle[42496:3307472] SecondViewController viewWillDisappear
2018-08-09 20:23:37.193930+0800 LifeCycle[42496:3307472] ViewController viewWillAppear
2018-08-09 20:23:37.698928+0800 LifeCycle[42496:3307472] SecondViewController viewDidDisappear
2018-08-09 20:23:37.699078+0800 LifeCycle[42496:3307472] ViewController viewDidAppear
總結(jié):
頁面間push的顯示邏輯總是先觸發(fā)Disappear, 然后觸發(fā)Appear
其他
收到
didReceiveMemoryWarning
方法調(diào)用時,嗎我們需要把不是位于頂級視圖的view全部釋放掉(有些業(yè)務(wù)上必須留存的視圖除外), 以減輕內(nèi)存壓力; 恢復(fù)頁面時, Controller會重新走loadView
流程, 此時需要注意業(yè)務(wù)邏輯是否重復(fù)除了
init
方法, 其他方法在生命周期內(nèi)都可能多以調(diào)用loadView
實現(xiàn)該方法, 卻沒有給self.view
賦值的情況下, 不會產(chǎn)生根view