是否始终以编程方式或IB调用 initWithNibName ?
示例:
我有两个viewController,一个名为PhotoViewController,另一个名为ViewController。 PhotoViewController以编程方式创建视图,但取消注释initWithNibName。
这是我的PhotoViewController.m:
- (void)loadView
{
NSLog(@"loadView in PhotoView");
UIView *view = [[UIView alloc] initWithFrame:[UIScreen
mainScreen].applicationFrame];
[view setBackgroundColor:[UIColor redColor]];
self.view = view;
[view release];
}
// Loading views from a nib file.
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
NSLog(@"PhotoViewController initWithNibName=%@",nibNameOrNil);
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {
// Custom initialization
}
return self;
}
这是我在ViewController.m中的调用
- (void) doSearchFlickr
{
PhotoViewController *pController = [[PhotoViewController alloc]init];
[self.view addSubview:pController.view];
[pController release];
}
然后我在日志中看到令我困惑的事情:
2011-10-23 10:52:52.151 TableViewPG[1192:b303] PhotoViewController initWithNibName=(null)
2011-10-23 10:52:52.153 TableViewPG[1192:b303] loadView in PhotoView
根据第30页的ViewControllerPGforiPhoneOS,如果我以编程方式覆盖loadView,则不应调用initWithNibName。
我的逻辑中是否有任何缺陷?
答案 0 :(得分:2)
-[UIViewController init]
只执行[self initWithNibName:nil bundle:nil]
。您可以通过在-[PhotoViewController initWithNibName:bundle:]
中放置一个断点并查看调用堆栈来轻松检查这一点。
您的-[PhotoViewController loadView]
没问题。
View Controller Programming Guide for iOS在第30页上没有说明initWithNibName:bundle:
。