Xib類型VC
Log日志:
2019-10-12 15:44:51.179910+0800 -[XibSelfViewController init]
2019-10-12 15:44:51.180079+0800 -[XibSelfViewController initWithNibName:bundle:]
2019-10-12 15:44:51.182801+0800 -[XibSelfViewController loadView]
2019-10-12 15:44:51.182801+0800 -[XibSelfViewController viewDidLoad]
2019-10-12 15:44:51.185491+0800 -[XibSelfViewController viewWillAppear:]
2019-10-12 15:44:51.187205+0800 -[XibSelfViewController viewWillLayoutSubviews]
2019-10-12 15:44:51.187349+0800 -[XibSelfViewController viewDidLayoutSubviews]
2019-10-12 15:44:51.689125+0800 -[XibSelfViewController viewDidAppear:]
分析:
init
: 對象初始化方法
initWithNibName:bundle:
: 先看一下, Apple官方的注釋
/*The designated initializer. If you subclass UIViewController, you must call the super implementation of this
method, even if you aren't using a NIB. (As a convenience, the default init method will do this for you,
and specify nil for both of this methods arguments.) In the specified NIB, the File's Owner proxy should
have its class set to your view controller subclass, with the view outlet connected to the main view. If you
invoke this method with a nil nib name, then this class' -loadView method will attempt to load a NIB whose
name is the same as your view controller's class. If no such NIB in fact exists then you must either call
-setView: before -view is invoked, or override the -loadView method to set up your views programatically.
*/
VC量身定做的初始化器.如果你要繼承UIViewController, 必須回調(diào)父類的此方法, 即時你不使用NIB.
(為了方便, 默認(rèn)的init方法會為你自動做這一步, 但該方法的兩個參數(shù)均為nil).
在與UIViewController綁定的NIB內(nèi), the File's Owner class選項應(yīng)當(dāng)
設(shè)置為你自己要編寫的類, 并且別忘了把NIB的view outlets連接到the File's Owner.
如果你用一個nil的nib name調(diào)用此方法, 該類的 -loadview 方法將會嘗試去加載一個與該類名字相同的NIB文件.
如果沒有該NIB文件, 你必須在-view調(diào)用之前調(diào)用-setview方法或重寫-loadView方法來手動設(shè)置VC的view
loadView
: 它會加載或創(chuàng)建一個view并把它賦值給UIViewController的view屬性廷痘。在加載VC的view.loadView方法不應(yīng)該直接被調(diào)用荆针,而是由系統(tǒng)調(diào)用亲桥。
創(chuàng)建view的過程中往史,首先會根據(jù)nibName去找對應(yīng)的nib文件然后加載伙窃。如果nibName為空或找不到對應(yīng)的Nib文件择诈,則會創(chuàng)建一個空視圖(這種情況一般是純代碼), 如果使用Xib類型VC重寫此方法則必須設(shè)置好VC的view, 否則將會引起無限回調(diào).
在訪問VC的view時如果view為nil, 則會調(diào)用-loadView方法
注意:在重寫loadView方法的時候颖医,不要調(diào)用父類的方法备籽。
注意:假設(shè)我們在處理內(nèi)存警告時釋放view屬性:self.view = nil庐完。因此loadView方法在視圖控制器的生命周期內(nèi)可能被調(diào)用多次钢属。
注意: 如果nib類型的VC通知重寫了該方法, 則會以此方法返回的view為準(zhǔn)
viewDidLoad
: Vc 的 view加載完畢
viewWillAppear
: Vc 的 view即將展示
viewWillLayoutSubviews
: Vc 即將調(diào)用 view的-layoutSubviews方法
viewDidLayoutSubviews
: Vc 調(diào)用 view的-layoutSubviews方法完畢
viewDidAppear
: Vc 的 view已加載完畢