ViewController的生命周期包括:
Initialize
ViewDidLoad
ViewWillAppear
ViewDidAppear
ViewWillDisappear
ViewDidDisappear
ViewDidUnload
Dispose
對於Application來說,ViewController的每個階段都對應了一個方法,IOS會在適當的時間調用對應的方法,因此,我們可以在每個方法中添加對應的代碼來做我們想做的事。需要注意的是,這些方法都是為ViewController服務的,對於Views對象來說,是沒有這些方法的。
View的生命周期方法如下:
* ViewDidLoad - Called when you create the class and load from xib. Great for initial setup and one-time-only work
* ViewWillAppear - Called right before your view appears, good for hiding/showing fields or any operations that you want to happen every time before the view is visible. Because you might be going back and forth between views, this will be called every time your view is about to appear on the screen
* ViewDidAppear - Called after the view appears - great place to start an animations or the loading of external data from an API.
* ViewWill/DidDisappear - Same idea as the WillAppear.
* ViewDidUnload/Dispose - Available to you, In objective-c, this is where you do your cleanup and release of stuff, but this is handled automatically so not much you really need to do here.
Application的生命周期包括:
在IOS中,生命周期就是由一系列的方法構成, 對於一個App來說,涉及到的生命周期有:
1、點擊icon啟動,對應的方法有:
didFinishLaunchingWithOptions:
applicationDidBecomeActive:
2、按下Home鍵,返回桌面,對應方法:
applicationWillResignActive
applicationDidBecomeInactive
3、再點擊icon回到前台,對應方法:
applicationWillEnterForegroud
applicationDidBecomeActive
4、Application忽然被終止,對應方法:
applicationWillTerminate
這兒需要注意,點2下home鍵,把後台程序關掉不會調用這個方法,在蘋果的IOS開發指南上有如下描述:
”Even if you develop your application using iPhone SDK 4 and later, you must still be prepared for your application to be terminated. If memory becomes constrained, the system might remove applications from memory in order to make more room. If your application is currently suspended, the system removes your application from memory without any notice. However, if your application is currently running in the background, the system does call the applicationWillTerminate:method of the application delegate. Your application cannot request additional background execution time from this method.“ 就是說,在機器內存不足時,IOS會清理後台程序,在這個時候會調用該方法,一般情況下,很少會用到這個方法,盡量不要在這個方法裡寫你的應用邏輯。