You can pass any string name to initWithNibName:
. You are not just restricted to calling initWithNibName:@"MyClassName"
when your class is called MyClassName
. It could be initWithNibName:@"MyClassNameAlternateLayout"
.
This becomes useful if you need to load a different nib depending on what the app needs to do. While I try to have one nib per view controller per device category (iPhone or iPad) whenever possible to make development and maintenance simpler, I could understand if a developer would want to provide a different layout or different functionality at times.
Another important point is that initWithNibName:bundle: is the designated initializer for UIViewController
. When you call -[[UIViewController alloc] init]
, then initWithNibName:bundle:
is called behind the scenes. You can verify this with a symbolic breakpoint. In other words, if you simply want the default behavior, it is expected that you can call -[[UIViewController alloc] init]
and the designated initializer will be called implicitly.
If, however, you are calling -[[UIViewController alloc] init]
and not getting the expected behavior, it's likely that your UIViewController subclass has implemented - (id)init
incorrectly. The implementation should look like one of these two examples:
- (id)init
{
self = [super init];
if (self) {
// custom initialization
}
return self;
}
or
- (id)init
{
NSString *aNibName = @"WhateverYouWant";
NSBundle *aBundle = [NSBundle mainBundle]; // or whatever bundle you want
self = [self initWithNibName:aNibName bundle:aBundle];
if (self) {
// custom initialization
}
return self;
}