參考
http://www.cnblogs.com/wwwkhd/archive/2011/03/14/1983192.html
由init、loadView、viewDidLoad、viewDidUnload、dealloc的關係說起
init方法
在init方法中實例化必要的對象(遵從LazyLoad思想)
init方法中初始化ViewController本身
loadView方法
當view需要被展示而它卻是nil時,viewController會調用該方法。 不要直接調用該方法。
如果手工維護views,必須重載重寫該方法
如果使用IB維護views,必須不能重載重寫該方法
loadView和IB構建view
你在控制器中實現了loadView方法,那麼你可能會在應用運行的某個時候被內存管理控制調用。 如果設備內存不足的時候, view 控制器會收到didReceiveMemoryWarning的消息。 默認的實現是檢查當前控制器的view是否在使用。 如果它的view不在當前正在使用的view hierarchy裡面,且你的控制器實現了loadView方法,那麼這個view將被release, loadView方法將被再次調用來創建一個新的view。
viewDidLoad方法
viewDidLoad 此方法只有當view從nib文件初始化的時候才被調用。
重載重寫該方法以進一步定制view
在iPhone OS 3.0及之後的版本中,還應該重載重寫viewDidUnload來釋放對view的任何索引
viewDidLoad後調用數據Model
viewDidUnload方法
當系統內存吃緊的時候會調用該方法(注:viewController沒有被dealloc)
內存吃緊時,在iPhone OS 3.0之前didReceiveMemoryWarning是釋放無用內存的唯一方式,但是OS 3.0及以後viewDidUnload方法是更好的方式
在該方法中將所有IBOutlet(無論是property還是實例變量)置為nil(系統release view時已經將其release掉了)
在
該方法中釋放其他與view有關的對象、其他在運行時創建(但非系統必須)的對象、在viewDidLoad中被創建的對象、緩存數據等release對
像後,將對象置為nil(IBOutlet只需要將其置為nil,系統release view時已經將其release掉了)
一般認為viewDidUnload是viewDidLoad的鏡像,因為當view被重新請求時,viewDidLoad還會重新被執行
viewDidUnload中被release的對象必須是很容易被重新創建的對象(比如在viewDidLoad或其他方法中創建的對象),不要release用戶數據或其他很難被重新創建的對象
dealloc方法
viewDidUnload和dealloc方法沒有關聯,dealloc還是繼續做它該做的事情
參考圖從APPLE文件上截出
原文資料
|
Task
|
Methods
|
Discussion
|
|
Allocating critical
data structures
required by your
view controller
|
Initialization methods
|
Your custom initialization method (whether it is named
init or something else) is always responsible for
putting your view controller object in a known good
state. This includes allocating whatever data structures
are needed to ensure proper operation.
|
|
Creating your view
objects
|
loadView
|
Overriding the loadView method is required only if
you intend to create your views programmatically. If
you are using storyboards, the views are loaded
automatically from the storyboard file.
|
|
Allocating or
loading data to be
displayed in your
view
|
viewDidLoad
|
Typically, data objects are provided by configuring
your view controller’s properties. Any additional data
objects your view controller wants to create should
be done by overriding the viewDidLoad method. By
the time this method is called, your view objects are
guaranteed to exist and to be in a known good state.
|
|
Releasing
references to view
objects
|
viewDidUnload
dealloc
|
If you maintain strong references to any view objects
in your view hierarchy using outlets or other instance
variables in your class, you must always release those
references when the views are no longer needed. You
release a view object by setting your outlet or variable
to nil. For more information about when views get
released, see “Understanding How Views Are Loaded
and Unloaded” (page 57).
|
|
eleasing data
that is not needed
when your view is
not displayed
|
viewWillUnload
viewDidUnload
|
You can use the viewDidUnload method to
deallocate any data that is view specific and that can
be re-created easily enough if the view is loaded into
memory again. If re-creating the data might be too
time-consuming, though, you do not have to release
the corresponding data objects here. Instead, you
should consider releasing those objects in your
didReceiveMemoryWarning method.
|
|
Responding to
low-memory
notifications
|
didReceiveMemory-
Warning
|
Use this method to deallocate all noncritical custom
data structures associated with your view controller.
Although you would not use this method to release
references to view objects, you might use it to release
any view-related data structures that you did not
already release in your viewDidUnload method. ( The
view objects themselves should always be released
in the viewDidUnload method.)
|
|
Releasing critical
data structures
required by your
view controller
|
dealloc
|
Override this method only to perform any last-minute
cleanup of your view controller class. Objects stored
in instance variables and properties are automatically
released; you do not need to release them explicitly.
|

沒有留言:
張貼留言