initWithNibName 和 loadNibNamed 的区别

原文地址:http://blog.csdn.net/dizzthxl/article/details/9031255

UIViewController initWithNibName
这时候是延迟加载
 
主要代码如下:
 

            ShowViewController * showMessage = [[ShowViewController alloc]

                                                initWithNibName:@"ShowViewController" bundle:nil];

            self.showViewController = showMessage;

            [showMessage release];

这时候是延迟加载,这个View上的控件是 nil 的
 
self.showViewController.show_Topic 
只有到 需要显示时,才会不是 nil

 [saveViewController.view removeFromSuperview];

[self.view insertSubview:showViewController.view atIndex:0];

 
这时候 xib 文件的要求:
 
 
NSBundle loadNibNamed
 
实时加载。
这时候的代码:

NSArray * nib = [[NSBundle mainBundle] loadNibNamed:@"Save3ViewController" 

                                                          owner:self options:nil] ;

self.showViewController = [nib lastObject];

这时候的xib文件
 
 
 
 
 
如果用 loadNibNamed 加载 initWithNibName 用到格式的 xib 文件,就会报错误:
 
setValue:forUndefinedKey
loaded the...nib but the view outlet was not set
 
 
 
 
loadNibNamed和initWithNibName需要加载的xib文件是不一样的。initWithNibName需要加载的xib文件的File Owner应改是需要加载的类,
而loadNibNamed需要加载的xib文件的File Owner为NSObject。
http://www.cnblogs.com/GoGoagg/archive/2011/06/09/2076456.html
 
 
when using loadNibNamed:owner:options:, the File's Owner should be NSObject, the main view should be your class type, and all outlets should be hooked up to the view, not the File's Owner.
http://stackoverflow.com/questions/46220/iphone-app-crashing-error-question
 
 
 
参考资料:

从xib/nib加载UIViewController/或其子类
http://www.cnblogs.com/GoGoagg/archive/2011/06/09/2076456.html

iPhone App Crashing - Error Question
http://stackoverflow.com/questions/46220/iphone-app-crashing-error-question
 
 
 
关于 initWithNibName 和 loadNibNamed 的区别和联系。之所以要把这两者来一起讲,我觉的我也有点困惑,到底用那种?其实真正搞清楚了他们之间的差别,就不会这么迷惘了。因为这两个方法,根本就不是一路货色。
 
既然,是要说明这2个方法,那就着重将区别吧。
但是第一步,还是要罗嗦一下,他们的联系可以使用此方法加载用户界面(xib文件)到我们的代码中,这样,可以通过操作这个加载进来的(xib)对象,来操作xib文件内容。
 
下面进入主题,谈区别:
1. ShowViewController的initWithNibName方法
ShowViewController * showMessage = [[ShowViewController alloc]
 
                                                initWithNibName:@"ShowViewController" bundle:nil];
 
            self.showViewController = showMessage;
 
            [showMessage release];
 
关于 <wbr>initWithNibName <wbr>和 <wbr>loadNibNamed <wbr>的区别和联系-iPhone成长之路


2.VideoCellController的loadNibNamed方法
NSArray * nib = [[NSBundle mainBundle] loadNibNamed:@"Save3ViewController" 
                                                          owner:self options:nil] ;
self.showViewController = [nib lastObject];
[nib objectAtIndex:0];
 
关于 <wbr>initWithNibName <wbr>和 <wbr>loadNibNamed <wbr>的区别和联系-iPhone成长之路

总结:
只看他们初始化,那可能感觉是一样的。但是如果,打开分别看xib的关系的时候,才恍然大悟,原来他们的集成类都不一样。
1. initWithNibName要加载的xib的类为我们定义的视图控制器类 
  loadNibNamed要加载的xib的类为NSOjbect。
(比如:甲,乙都买了一个iPhone,但是,甲的是自己的钱,而乙用的是某某的钱)
 
2.加载方式不同
 initWithNibName方法:是延迟加载,这个View上的控件是 nil 的,只有到 需要显示时,才会不是 nil
loadNibNamed方法:即时加载,用该方法加载的xib对象中的各个元素都已经存在。
(认真理解这句帮规:when using loadNibNamed:owner:options:, the File's Owner should be NSObject, the main view should be your class type, and all outlets should be hooked up to the view, not the File's Owner.)


关于 <wbr>initWithNibName <wbr>和 <wbr>loadNibNamed <wbr>的区别和联系-iPhone成长之路关于 <wbr>initWithNibName <wbr>和 <wbr>loadNibNamed <wbr>的区别和联系-iPhone成长之路
 
 

 

posted on 2015-06-01 09:46  whqfor  阅读(409)  评论(0编辑  收藏  举报