码迷,mamicode.com
首页 > 移动开发 > 详细

iOS UIViewController 和 nib 相关的3个方法

时间:2015-08-21 10:48:07      阅读:133      评论:0      收藏:0      [点我收藏+]

标签:

iOS UIViewController 的 awakeFromNib 以及 - (id)initWithCoder:(NSCoder *)aDecoder 和 - (instancetype)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil

 

 

首先看一下awakeFromNib的官方文档:

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.


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. 

 

对于UIviewController来说,awakeFromNib 和 - (id)initWithCoder:(NSCoder *)aDecoder是一起出现的,先调用- (id)initWithCoder:(NSCoder *)aDecoder 再调用 awakeFromNib。用storyboard 方法创建的UIViewcontroller就会产生这样的效果。

 

但是对于使用以前的初始化函数 - (instancetype)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil来生成UIViewcontroller是不会调用上面2个函数的!问什么呢?这其实是2套初始化方法,第一套使用storyboard 方法创建的UIViewcontroller,确实是从nib文件中decode出了 viewcontroller;第二套方法,也用到了nib,但是这个nib仅仅包含view的信息,根本没有包含viewcontroller对象,viewcontroller对象不是从nib中decode出来的!

 

iOS UIViewController 和 nib 相关的3个方法

标签:

原文地址:http://www.cnblogs.com/breezemist/p/4747039.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!