initWithNibName 和 loadNibNamed 的区别

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

  • 0
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值