首页 > 技术文章 > 转:awakeFromNib/loadView/viewDidLoad总结

UncleDraw 2016-07-25 15:07 原文

转自:http://blog.csdn.net/sxfcct/article/details/7840969

 

每个ios开发者对loadViewviewDidLoad肯定都很熟悉,虽然这两个函数使用上真的是非常简单,但是和类似的initWithNibName/awakeFromNib/initWithCoder放在一起还是非常容易让人混淆的.

一、loadView

永远不要主动调用这个函数。view controller会在view的property被请求并且当前view值为nil时调用这个函数。如果你手动创建view,你应该重载这个函数,且不要在重载的时候调用[super loadview]。如果你用IB创建view并初始化view controller,那就意味着你使用initWithNibName:bundle:方法,这时,你不应该重载loadView函数。

这个方法系统的默认实现是这样:

1;寻找有关可用的nib文件的信息,根据这个信息来加载nib文件       //所以,nib的加载过程是在loadview中完成的哦。

2;如果没有有关nib文件的信息,默认创建一个空白的UIView对象,然后把对象成赋值给viewcontroller的主view。

所以,如果你决定重载这个函数时,你也应该完成这些步骤:

把子类的view赋给view属性(property)(你create的view必须是唯一的实例,并且不被其他任何controller共享),而且你重载的这个函数不应该调用super,这个也是为了保持主view与controller的单一映射关系。

二、viewDidLoad

这个函数在controller加载了相关的views后被调用,而不论这些views存储在nib文件里还是在loadView函数中生成。

这个函数的作用主要是让你可以进一步的初始化你的views。viewDidLoad通常负责的是view及其子view被加载进内存之后的数据初始化的工作,即视图的数据部分的初始化。在iOS 3.0以及更高版本中,你应该重载viewDidUnload函数来释放任何对view的引用或者它里面的内容(子view等等)。

其多数情况下是做nib文件的后续工作。

三、viewDidUnload

这个函数是viewDidLoad的对立函数。在程序内存欠缺时,这个函数被controller调用,来释放它的view以及view相关的对象。由于controller通常保存着view以及相关object的引用,所以你必须使用这个函数来放弃这些对象的所有权以便内存回收。但不要释放那些难以重建的数据。

通常controller会保存nib文件建立的views的引用,但是也可能会保存着loadView函数创建的对象的引用。最完美的方法是使用合成器方法:

1
self.myCertainView = nil;

这样合成器会release这个view,如果你没有使用property,那么你得自己显式释放这个view。

四、结论

所以流程应该是这样:

loadView来加载view(无论nib文件或自定义的views)到内存 ——>viewDidLoad函数进一步初始化这些view (通常是侧重于数据data的初始化)——>内存不足时,调用viewDidUnload函数释放views

—->当需要使用view时又回到第一步

如此循环

    需要厘清两个概念,创建一个类和实例化一个类.在XCode中创建一个类和实例化一个类很容易区分,但是在IB(Interface Builder)中有时候就会迷糊.其实也很好区分,孤零零地创建了一个nib文件,没有和其他可被实例化的类有直接或间接关系的时候,这个类或这些类(一个nib文件也可能包含多个类)是没有机会被实例化的,所以这种情况只是通过ib创建了一个类,而没有实例化.真正的实例化还需要通过在Xcode用代码来读取这个nib文件.知道这两这的区别后这些方法也就容易辨认多了

viewDidLoad其实没什么可混淆的,无论通过什么途径加载(Xcode或者IB,这里的加载属于实例化)完view后肯定会执行这个方法.

loadView需要分两种情况.当你通过Xcode实例化一个类的时候就需要自己在controller中实现这个方法.而在IB中实例化就不需要实现它.

initWithNibName这个方法是在controller的类在IB中创建,但是通过Xcode实例化controller的时候用的.

initWithCoder是一个类在IB中创建但在xocdde中被实例化时被调用的.比如,通过IB创建一个controller的nib文件,然后在xcode中通过initWithNibName来实例化这个controller,那么这个controller的initWithCoder会被调用.

awakeFromNib 
当.nib文件被加载的时候,会发送一个awakeFromNib的消息到.nib文件中的每个对象,每个对象都可以定义自己的awakeFromNib函数来响应这个消息,执行一些必要的操作。也就是说通过nib文件创建view对象时执行awakeFromNib.awakeFromNib在loadView前被调用。

viewDidLoad 
当view对象被加载到内存后就会执行viewDidLoad,所以不管通过nib文件还是代码的方式创建对象都会执行viewDidLoad 。

 

 

 

- (void)loadView

Description Creates the view that the controller manages.

You should never call this method directly. The view controller calls this method when its view property is requested but is currently nil. This method loads or creates a view and assigns it to the view property.

If the view controller has an associated nib file, this method loads the view from the nib file. A view controller has an associated nib file if the nibName property returns a non-nil value, which occurs if the view controller was instantiated from a storyboard, if you explicitly assigned it a nib file using the initWithNibName:bundle: method, or if iOS finds a nib file in the app bundle with a name based on the view controller'€™s class name. If the view controller does not have an associated nib file, this method creates a plain UIView object instead.

If you use Interface Builder to create your views and initialize the view controller, you must not override this method.

You can override this method in order to create your views manually. If you choose to do so, assign the root view of your view hierarchy to the view property. The views you create should be unique instances and should not be shared with any other view controller object. Your custom implementation of this method should not call super.

If you want to perform any additional initialization of your views, do so in the viewDidLoad method.

 

 

- (void)viewDidLoad

Description Called after the controller'€™s view is loaded into memory.

This method is called after the view controller has loaded its view hierarchy into memory. This method is called regardless of whether the view hierarchy was loaded from a nib file or created programmatically in the loadView method. You usually override this method to perform additional initialization on views that were loaded from nib files.

 

- (void)awakeFromNib

Description Prepares the receiver for service after it has been loaded from an Interface Builder archive, or nib file.

The nib-loading infrastructure sends an awakeFromNib message to each object recreated from a nib archive, but only after all the objects in the archive have been loaded and initialized. When an object receives an awakeFromNib message, it is guaranteed to have all its outlet and action connections already established.

You must call the super implementation of awakeFromNib to give parent classes the opportunity to perform any additional initialization they require. Although the default implementation of this method does nothing, many UIKit classes provide non-empty implementations. You may call the super implementation at any point during your own awakeFromNib method.

Note:Note

During Interface Builder’s test mode, this message is also sent to objects instantiated from loaded Interface Builder plug-ins. Because plug-ins link against the framework containing the object definition code, Interface Builder is able to call their awakeFromNib method when present. The same is not true for custom objects that you create for your Xcode projects. Interface Builder knows only about the defined outlets and actions of those objects; it does not have access to the actual code for them.

During the instantiation process, each object in the archive is unarchived and then initialized with the method befitting its type. Objects that conform to the NSCoding protocol (including all subclasses of UIView and UIViewController) are initialized using their initWithCoder: method. All objects that do not conform to the NSCoding protocol are initialized using their init method. After all objects have been instantiated and initialized, the nib-loading code reestablishes the outlet and action connections for all of those objects. It then calls the awakeFromNib method of the objects. For more detailed information about the steps followed during the nib-loading process, see Nib Files in Resource Programming Guide.

Important:Important

Because the order in which objects are instantiated from an archive is not guaranteed, your initialization methods should not send messages to other objects in the hierarchy. Messages to other objects can be sent safely from within an awakeFromNib method.

Typically, you implement awakeFromNib for objects that require additional set up that cannot be done at design time. For example, you might use this method to customize the default configuration of any controls to match user preferences or the values in other controls. You might also use it to restore individual controls to some previous state of your application.

 

推荐阅读