iOS6及以前版本的iOS中UIViewController的生命週期(Load及Unload簡析)

(一)alloc以及初始化html

這沒有什麼好說的,通常來講主要有從nib初始化和本身用代碼初始化兩種。ios

從nib初始化調用下面的方法:安全

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil;app

若是本身用代碼初始化通常我是用[super initWithNibName:nil bundle:nil],以後再添加本身的代碼來初始化。ide

在初始化的過程當中應該僅僅去處理一些和view無關的數據和資源,view相關的資源應該在後續viewDidLoad的時候再去處理。ui

(二)加載view的過程this

當你controller的view屬性被第一次請求時,若是這時候view還不在內存裏,就會觸發對應的加載view的事件。先看下下圖:spa

loading_a_view_into_memory

首先被調用的是loadView方法,這個方法是加載view的過程,若是你沒有特殊須要不要亂重載此方法。loadView會判斷並使用正確的代碼來建立好一個view(見圖),建立好了以後就觸發viewDidLoad方法,這時候咱們就能夠作一些加載view以後的自定義操做了。viewDidLoad也處理完以後,controller的view屬性就準備好了能夠被各處調用了。翻譯

(三)釋放view的過程code

首先要說的是在iOS 6裏面已經不會在收到memoryWarning的時候自動釋放controller的view屬性了,這點是和以前不用的必定要注意。系統會處理掉一些繪圖用主要資源,來保證view所使用的內存儘可能的小,因此通常狀況下不須要太關心內存緊張的問題。一些釋放資源的操做要從viewDidUnload裏面挪出來放到didReceiveMemoryWarning裏面了,iOS 6裏面viewDidUnload這個事件已經被廢棄不會再被觸發了。

固然若是你很肯定你想要在收到內存警告的時候釋放掉view,能夠參照下面的官方代碼:

1

2

3

4

5

6

7

8

9

10

11

12

13

14

- (void)didReceiveMemoryWarning

{

    [super didReceiveMemoryWarning];

    // Add code to clean up any of your own resources that are no longer necessary.

    if ([self.view window] == nil)

    {

        // Add code to preserve data stored in the views that might be

        // needed later.

 

        // Add code to clean up other strong references to the view in

        // the view hierarchy.

        self.view = nil;

    }

}

比較合理的方法是爲一些能夠安全釋放的資源去建立成對的loadXXX和unloadXXX方法,而後在memoryWarning裏面去調用unloadXXX,在這些資源不在內存中的時候再去調用loadXXX。

仍是要說一說在iOS 6以前的系統裏view的釋放過程,先看圖:

unloading_a_view_controllers_view

在收到memoryWarning的時候系統會自動去嘗試釋放一個view,流程就像圖裏的樣子很簡單易懂。在有viewDidUnload這個事件的支持下,之前的viewDidLoad和它正好是配對的,因此能夠作一些配對操做的,不過如今就不能夠啦,要按照上面我說的方法本身作一個配對處理。

(四)最後蘋果有給出一張表列出具體的內存處理的時機,略懶在此就不翻譯了各位能夠參考一下:

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.
Creating custom objects Custom properties and methods Although you are free to use other designs, consider using a pattern similar the loadView method. Create a property that holds the object and a matched method to initialize it. When the property is read and its value is nil, call the associated load method.
Allocating or loading data to be displayed in your view viewDidLoad Data objects are typically 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.
Responding to low-memory notifications didReceiveMemoryWarning Use this method to deallocate all noncritical objects associated with your view controller. On iOS 6, you can also use this method to release references to view objects.
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.
相關文章
相關標籤/搜索