IOS中UIView的生命週期

來源:互聯網
上載者:User

UIView對象一般都是由alloc和init方法建立的,由UIViewController進行維護和管理。一般需要完 成的任務有:建立視圖,顯示視圖,隱藏視圖和釋放視圖。

一般視圖不會在程式運行時立刻建立 並載入,只有當應用需要這個視圖的時候才會建立,這種延遲建立視圖的方法能夠提高記憶體的使用效率 。同樣,在需要這個視圖之前,系統會檢查是不是已經載入過這個視圖,如果是,直接返回建立過的 view,如果沒有,則開始建立過程

- (void)loadView

載入視圖過程中執行代碼

- (void)viewDidLoad

視圖載入完成需要執行的代碼

- (void)viewDidUnload

卸載視圖代 碼

一個視圖切換到另一個視圖的時候,之前那個視圖並不會被清除,還會留在記憶體中等待系統調 用,所以一個視圖在使用到卸載過程中雖然可能多次出現,但是只會載入一次,也就是調用一次 loadView和viewDidLoad方法。

在IOS6以前,在系統接收到Memory Warning時,會調用 viewDidUnload這個方法,對視圖進行卸載清空。但是IOS6以後這個方法已經不再被使用了,文檔裡是這 麼描述的

這裡給出的解釋是, 視圖不再在低記憶體的情況下被清除了。蘋果公司WWDC2012對這個方法的告別陳述

The method viewWillUnload and viewDidUnload. We’re not going to call them anymore. I mean, there’s kind of a cost-benifit equation and analysis that we went through. In the early days, there was a real performance need for us to ensure that on memory warnings we unloaded views. There was all kinds of graphics and backing stores and so forth that would also get unloaded. We now unload those independently of the view, so it isn’t that big of a deal for us for those to be unloaded, and there were so many bugs where there would be pointers into。

具體為什麼不需要清空view呢,下面解釋了原因(摘自唐巧的《再見,viewDidUnload方法》 )

1.UIView有一個CALayer的成員變數,CALayer是具體用於將自己畫到螢幕上的。如下圖所示:

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.