iOS6及之前版本的iOS中UIViewController的生命周期(Load及Unload简析)

(一)alloc以及初始化

这没有什么好说的,一般来说主要有从nib初始化和自己用代码初始化两种。

从nib初始化调用下面的方法:

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

如果自己用代码初始化一般我是用[super initWithNibName:nil bundle:nil],之后再添加自己的代码来初始化。

在初始化的过程中应该仅仅去处理一些和view无关的数据和资源,view相关的资源应该在后续viewDidLoad的时候再去处理。

(二)加载view的过程

当你controller的view属性被第一次请求时,如果这时候view还不在内存里,就会触发对应的加载view的事件。先看下下图:

loading_a_view_into_memory

首先被调用的是loadView方法,这个方法是加载view的过程,如果你没有特殊需要不要乱重载此方法。loadView会判断并使用正确的代码来创建好一个view(见图),创建好了之后就触发viewDidLoad方法,这时候我们就可以做一些加载view之后的自定义操作了。viewDidLoad也处理完之后,controller的view属性就准备好了可以被各处调用了。

(三)释放view的过程

首先要说的是在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.


你可能感兴趣的:(iOS6及之前版本的iOS中UIViewController的生命周期(Load及Unload简析))