UIViewController init vs initWithNibName: bundle:

In my application, I click on the view controller (UITableViewController), which also has a property / output that references the UITableViewCell. It seems like creating a controller using:

PreferencesController *pController = [[PreferencesController alloc] init]; 

does not create an object for UITableViewCell in the xib file, so the output is null, so loading the table throws an exception. I solved this with:

 PreferencesController *pController = [[PreferencesController alloc] initWithNibName:@"PreferencesController" bundle:nil]; 

but I really didn’t understand why this worked, as from the documentation it seems that init should be enough to load the linked nib file (PreferencesController.xib).

+5
ios uitableview uiviewcontroller init
source share
3 answers

Something seems to be the magic name of PreferencesController . I had exactly the same problem. Renaming my class (and xib) to something else solved the problem.

+4
source share

Edit: I was incorrect, nib files should automatically load using alloc init if they are named the same as the controller.

What is your file owner in the Builder interface? You can change the default behavior by changing this value.

+3
source share

You must override initWithNibName:bundle: instead of init because it is a "designated initializer." When you load this from a Nib file, this is the creator's calling message.

 - (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil { if (self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil]) { // Custom initialization } return self; } 

Resources

+1
source share

All Articles