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.
相关文章
相关标签/搜索