目錄:[ - ] html
文字參考:stackoverflow.com ios
In general, this is what I do: web
1) ViewDidLoad - 須要的視圖元素都在此方法加載,例如視圖是一個Form,有3個label,這個方法一次成型(Whenever I'm adding controls to a view that should appear together with the view, right away, I put it in the ViewDidLoad method. Basically this method is called whenever the view was loaded into memory. So for example, if my view is a form with 3 labels, I would add the labels here; the view will never exist without those forms.) app
2) ViewWillAppear: 這裏一般不作視圖的修改,而用來更新Form數據,就是給給form從新填入新數據,該頁面從別的頁面回來時更新。UIView的建立很是費時費力,所以到 viewWillAppear時,iphone已經蓄勢待發地、興沖沖地要去顯示了,就不要在這個地方再幹費時費力的事情了,純更新數據就能夠了。(I use ViewWillAppear usually just to update the data on the form. So, for the example above, I would use this to actually load the data from my domain into the form. Creation of UIViews is fairly expensive, and you should avoid as much as possible doing that on the ViewWillAppear method, becuase when this gets called, it means that the iPhone is already ready to show the UIView to the user, and anything heavy you do here will impact performance in a very visible manner (like animations being delayed, etc).) dom
3) ViewDidAppear: 最後,視圖已經顯示了,那這裏能夠作一些遠程取數據的費時費力的工做。(Finally, I use the ViewDidAppear to start off new threads to things that would take a long time to execute, like for example doing a webservice call to get extra data for the form above.The good thing is that because the view already exists and is being displayed to the user, you can show a nice "Waiting" message to the user while you get the data.) iphone
============================================================== ide
文字來源:UIViewController類參考 動畫
- (void)viewDidLoad ui
該方法在控制器的視圖載入內存時調用 this
Discussion
該方法在視圖控制器(XXXViewController)已經將其視圖樹(視圖裏應有的各類東西)(view hierarchy)載入內存後調用。該方法的調用不考慮是否視圖樹是從nib文件(或storyboard)得來,仍是純代碼得到;純代碼通常使用 loadView 方法,咱們通常經過重寫 loadView 方法作附加初始化,附加是指基於已有的 nib 文件或 storyboard。
- (void)viewWillAppear:(BOOL)animated
該方法通知視圖控制器,它的視圖將要(is about to) 被加入到視圖樹。
該方法兩個事件以前調用,一個是在視圖將要被加入到視圖樹,二個是任何所須要的動畫配置以前。咱們能夠重寫該方法,實現定製如何顯示視圖,好比,使用該方法改變狀態條(status bar)的方向和樣式,以適應該視圖啓動時的方向和樣式。重寫該方法必須調用 [super viewWillAppear:animated]。.
For more information about the how views are added to view hierarchies by a view controller, and the sequence of messages that occur, see 「Responding to Display-Related Notifications」.
該方法通知視圖控制器,它的視圖已經被加入視圖樹
咱們能夠重寫該方法執行附加任務,實現如何顯示出視圖。重寫必須調用 super 方法。