Is the initWithNibName always called whenever programmatically or IB?
Example:
I have two viewController, one named PhotoViewController, the other named ViewController.
The PhotoViewController creates view programmatically, but with initWithNibName uncommented.
Here is my 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;
}
Here is my call in ViewController.m
- (void) doSearchFlickr
{
PhotoViewController *pController = [[PhotoViewController alloc]init];
[self.view addSubview:pController.view];
[pController release];
}
Then I see something confusing me in the log:
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
According to the ViewControllerPGforiPhoneOS on page 30, if I override loadView programmatically, initWithNibName should not be called.
Is there any flaw(s) in my logic?
-[UIViewController init]just executes[self initWithNibName:nil bundle:nil]. You can easily check this by putting a breakpoint in your-[PhotoViewController initWithNibName:bundle:]and looking at the call stack.Your
-[PhotoViewController loadView]is fine.The View Controller Programming Guide for iOS doesn’t say anything about
initWithNibName:bundle:on page 30.