首先看一下官方解釋:
- (void)loadView; // This is where subclasses should create their custom view hierarchy if they aren't using a nib. Should never be called directly.
- (void)viewDidUnload NS_DEPRECATED_IOS(3_0,6_0); // Called after the view controller's view is released and set to nil. For example, a memory warning which causes the view to be purged. Not invoked as a result of -dealloc.
- (void)viewDidLoad; // Called after the view has been loaded. For view controllers created in code, this is after -loadView. For view controllers unarchived from a nib, this is after the view is set.
- (void)viewWillAppear:(BOOL)animated; // Called when the view is about to made visible. Default does nothing
- (void)viewDidAppear:(BOOL)animated; // Called when the view has been fully transitioned onto the screen. Default does nothing
- (void)viewWillDisappear:(BOOL)animated; // Called when the view is dismissed, covered or otherwise hidden. Default does nothing
- (void)viewDidDisappear:(BOOL)animated; // Called after the view was dismissed, covered or otherwise hidden. Default does nothing
-viewDidLoad,這個方法在view被載入之後呼叫,如果沒有nib檔案,這個方法是在loadView方法被呼叫之後呼叫,如果有nib檔案,這個方法是在view從nib中載入後呼叫,總之當一個viewController被初始化後,-viewDidLoad方法僅被呼叫一次。
-viewWillAppear,在載入了view之後,在view出現在螢幕之前呼叫viewWillAppear
--viewDidAppear,view出現在螢幕之後開始呼叫
-viewWillDisappear,是指view將要消失的時候開始呼叫
-viewDidDisappear,是指view已經消失後開始呼叫
-viewDidUnload,是指一個view被release或被記憶體警告清除時呼叫,在dealloc後不會呼叫
我們會把一次執行的程式碼放在viewDidLoad裡面,把要多次執行的程式碼放在viewWillAppear裡面