本文的Demo工程代碼參考這里的StudyUIViewAndUIVC
目錄
加載
加載的完整過程: 初始化 -> loadView -> viewDidLoad -> viewWillAppear -> viewWillLayoutSubviews -> viewDidLayoutSubviews -> viewDidAppear
初始化
- 原型
UIViewController初始化有以下三個方法
- (id)init;
- (instancetype)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil;
- (instancetype)initWithCoder:(NSCoder *)aDecode;
- 觸發(fā)
當(dāng)通過init或new方法創(chuàng)建UIViewController對象時, 會依次調(diào)用
init -> initWithNibName:bundle:
當(dāng)通過storyboard直接由系統(tǒng)創(chuàng)建UIViewController對象時, 會調(diào)用
initWithCoder:
關(guān)于初始化更準(zhǔn)確的原文描述請參考Apple Developer API Reference
loadView
- 原型
- (void)loadView;
- 觸發(fā)
The view controller calls this method when its view property is requested but is currently nil
- 備注
1: 不要直接調(diào)用該方法!!!
2: 如果使用代碼維護(hù)views, 必須重寫該方法; 如果使用Interface Builder維護(hù)views, 不能重寫該方法
viewDidLoad
- 原型
- (void)viewDidLoad;
- 觸發(fā)
This method is called after the view controller has loaded its view hierarchy into memory
viewWillAppear
- 原型
- (void)viewWillAppear:(BOOL)animated;
- 觸發(fā)
This method is called before the view controller'??s view is about to be added to a view hierarchy and before any animations are configured for showing the view
viewWillLayoutSubviews
- 原型
- (void)viewWillLayoutSubviews;
- 觸發(fā)
This method is called every time the frame changes like for example when rotate or it’s marked as needing layout
viewDidLayoutSubviews
- 原型
- (void)viewDidLayoutSubviews;
- 觸發(fā)
When the bounds change for a view controller'??s view, the view adjusts the positions of its subviews and then the system calls this method
viewDidAppear
- 原型
- (void)viewDidAppear:(BOOL)animated;
- 觸發(fā)
This method is executed after the animation displaying the view finishes so in this step the view is already visible for the user
- 備注
1: 在該方法適合做這些操作: 獲取數(shù)據(jù)(從數(shù)據(jù)存儲或網(wǎng)絡(luò))并顯示到view
卸載
卸載的完整過程: viewWillDisappear -> viewDidDisappear -> dealloc
viewWillDisappear
- 原型
- (void)viewWillDisappear:(BOOL)animated;
- 觸發(fā)
This method is called before the view is actually removed and before any animations are configured
viewDidDisappear
- 原型
- (void)viewDidDisappear:(BOOL)animated;
- 觸發(fā)
You can override this method to perform additional tasks associated with dismissing or hiding the view
dealloc
- 原型
- (void)dealloc;
- 觸發(fā)
在沒有內(nèi)存泄漏的情況下, dealloc是肯定會被調(diào)用的, 即使延遲釋放(例如autoreleasepool)情況下, 肯定也是會被調(diào)用的
所以很多內(nèi)存泄漏檢測工具就是基于此, 例如MLeaksFinder
- 備注
1: You override this method to dispose of resources other than the object’s instance variables
答疑
構(gòu)建view hierarchy要放在加載時的哪個方法中?
盡可能地使用Interface Builder維護(hù)views
如果必須使用代碼, 理論上需要放在loadView中, 但是因為
加載時的viewDidLoad方法中適合做什么操作?
viewDidLoad適合做與UI無關(guān)的數(shù)據(jù)和模型的操作
切記不要在viewDidLoad做view hierarchy和布局的操作
sub views布局要放在加載時的哪個方法中?
非autolayout的布局必須要放到viewWillLayoutSubviews
viewDidLoad中只確定了view的bounds, 而沒有確定orientation; viewWillLayoutSubviews中才真正確定了view的布局
哪些方法需要調(diào)用父類的方法?
這些方法必須調(diào)用父類的方法([super xyz]): viewWillAppear / viewDidAppear; viewDidDisappear / viewDidDisappear
如果父類是UIViewController, viewDidLoad方法可以不必調(diào)用父類的方法; 如果父類是自定義類, 根據(jù)實際情況決定是否要調(diào)用
關(guān)于是否需要調(diào)用父類方法更準(zhǔn)確的原文描述請參考Apple Developer API Reference的各接口說明
參考
更多文章, 請支持我的個人博客