IOS initWithNibName 和 loadNibNamed的区别

内容引用自:http://blog.sina.com.cn/s/blog_7b9d64af01018f2u.html

他们的联系:可以使用此方法加载用户界面(xib文件)到我们的代码中,这样,可以通过操作这个加载进来的(xib)对象,来操作xib文件内容。

下面进入主题,谈区别:

1. ShowViewController的initWithNibName方法

ShowViewController* showMessage = [[ShowViewController alloc]

 

                        initWithNibName:@"ShowViewController" bundle:nil];

 

       self.showViewController = showMessage;

 

       [showMessage release];

 

1.initWithNibName要加载的xib的类为我们定义的视图控制器类 

  loadNibNamed要加载的xib的类为NSOjbect。

(比如:甲,乙都买了一个iPhone,但是,甲的是自己的钱,而乙用的是某某的钱)

 

 

2.加载方式不同

 initWithNibName方法:是延迟加载,这个View上的控件是 nil 的,只有到 需要显示时,才会不是 nil

loadNibNamed方法:即时加载,用该方法加载的xib对象中的各个元素都已经存在。

(认真理解这句帮规:whenusing loadNibNamed:owner:options:, the File's Owner should be NSObject, themain view should be your class type, and all outlets should be hooked up to theview, not the File's Owner.)

 

原文地址:https://www.cnblogs.com/yuyu-2012/p/4775661.html