导航

initWithNibName 和 loadNibNamed 的区别

Posted on 2012-05-28 18:33  蝈蝈俊  阅读(4980)  评论(0编辑  收藏  举报
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