IOS view在UIViewController中的生命週期

理解view的生命週期:

在UIViewController中,view(黑體的view指的是controller的view屬性)有兩個循環:加載和卸載循環。當程序的一部分向controller請求view的指針且view不在內存中時,view會進入加載循環,controller會將view加載入內存。

當程序接收到內存警告時,controller會嘗試卸載view,在卸載循環中,controller嘗試釋放它的view對象並返回到原始的無view狀態(當它不在屏幕上顯示時,這個條件的判斷到底是根據view的結構來還是根據用戶視覺來,我尚不清楚),直到view下次被請求。

在加載卸載循環中,controller處理的大部分邏輯。但是如果我們的controller還“持有”着view的後代view時,或者還有其他後續操作需要進行時,我們可以重載特定函數(後面會介紹到)來另行處理。

加載循環:

  1. 程序請求了controller的view.

  2. 如果view當前不在內存中,controller調用loadview函數。

  3. loadView 進行如下操作:

    • 如果你重載了這個函數,你應該自己創建必要的views並且將一個非nil值賦給view屬性

    • 如果你沒有重載這個函數,默認實現會使用controller的nibName 和 nibBundle屬性來嘗試從nib文件加載view。如果沒有找到nib文件,它嘗試尋找一個與view controller類名匹配(viewControllerClassName.nib)的nib文件。

    • 如果沒有可用的nib文件,那麼它創建一個空的UIView作爲它的view

  4.  controller 調用  viewDidLoad 方法來執行一些加載時(加載時一詞,相對於編譯時、運行時)任務.

程序可以重載loadView 和 viewDidLoad來執行一些任務:

Figure 2-2  將view載入內存

viewloading_process

 

卸載循環:

  1. 程序收到內存警告.

  2. 每個view controller調用 didReceiveMemoryWarning:

    • If you override this method, you should use it to release any custom data that your view controller object no longer needs. You should not use it to release your view controller’s view. You must call super at some point in your implementation to perform the default behavior.(iOS3.0以後不建議重載這個函數來進行額外的清除操作,使用viewDidUnload)

    • 默認實現會在確定可以安全地釋放view時釋放掉view

  3. 如果controller釋放了它的view, 它會調用 viewDidUnload. .可以重載這個函數來進行額外的清理操作(不要清除view和那些加載循環中無法rebuild的數據)。

 

 



Figure 2-3  卸載循環

viewunloading_process 

 

 

 

 

Important: In iOS 3.0 and later, the viewDidUnload method is the preferred place to put any code related to cleaning up your views. You might also override the didReceiveMemoryWarning method to release temporary caches or other private data that is no longer needed when the view is released. If you do overridedidReceiveMemoryWarning, always call super to give the inherited version of the method a chance to release the view.

In iOS 2.2 and earlier, you must use the didReceiveMemoryWarning method to perform your view-related cleanup and to release any unneeded private data structures. The viewDidUnload method is available only in iOS 3.0 and later.

For more information about managing memory during low-memory conditions, see 

Advanced Memory Management Programming Guide.

看看viewDidUnload的函數文檔:

viewDidUnload

Called when the controller’s view is released from memory.

- (void)viewDidUnload

Discussion

When a low-memory condition occurs and the current view controller’s views are not needed, the system may opt to remove those views from memory. This method is called after the view controller’s view has been released and is your chance to perform any final cleanup. If your view controller stores references to the view or its subviews, you should use this method to release those references (if you retained the objects initially) and set those references to nil. You can also use this method to release any objects that you created to support the view but that are no longer needed now that the view is gone. You should not use this method to release user data or any other information that cannot be easily recreated.

Typically, a view controller stores references to objects using an outlet, which is a variable or property that includes the IBOutlet keyword and is configured using Interface Builder. A view controller may also store pointers to objects that it creates programmatically, such as in the viewDidLoad method. The preferred way to relinquish ownership of any object (including those in outlets) is to use the corresponding accessor method to set the value of the object to nil. However, if you do not have an accessor method for a given object, you may have to release the object explicitly. For more information about memory management practices, see Advanced Memory Management Programming Guide.

By the time this method is called, the view property is nil.

Special Considerations

If your view controller stores references to views and other custom objects, it is also responsible for relinquishing ownership of those objects safely in its dealloc method. If you implement this method but are building your application for iOS 2.x, your dealloc method should release each object but should also set the reference to that object to nil before calling super.

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章