I think this post will be useful to someone new to the iOS development. One of the most important and commonly used classes in Cocoa is
UIViewController. Creating any non-trivial application will involve subclass and creating your own view controller. There are a lot of topics and tutorials elsewhere, so I’m not going to repeat them here. But I think one important concept that seems to be overlooked is how to initialize the view controller from a nib bundle.
If you are only dealing with
UIViewController, you might have heard that a convenient method is could pass
initWithNibName:bundle: like this:
CustomViewController* customViewController = [[CustomViewController alloc] initWithNibName:nil bundle:nil]
If the nib name is the same as the view controller (i.e.
CustomViewController.xib), which often is the case, then this code will work fine. The reason is that when
nil is passed to the method, a search is done to try to find the matching nib file for the view controller to load. Some people say this is a reasonable way to load the nib file. I would recommend new developers to the iOS to stay away from depending on this behavior and be explicit about which nib file to load. Otherwise, you would run into this next problem.
UITableViewController is another commonly used class of iOS. It’s a subclass of the
UIViewController. If you try to do similar thing as what’s done above, you would run into some weird behavior.
CustomTableViewController* customTableViewController = [[CustomTableViewControlleralloc] initWithNibName:nil bundle:nil]
If you initialize the table view controller this way, whatever you customized in the Interface Builder for your table view would not show up when you run the program. The reason is that
UITableViewController behaves differently when
nil is pass to
initWithNibName:bundle:. It actually creates a new table view for the table view controller rather than try to search for the nib file.
UITableViewController will only load from nib file if you specify the proper nib name. I don’t know what the rational behind it is, but the only consistent way to have the view controllers work properly is to specify the nib name if you want to load them from a nib file:
CustomTableViewController* customTableViewController = [[CustomTableViewControlleralloc] initWithNibName:@"CustomTableViewController" bundle:nil]
The details (and differences) of init from nib for these two classes are specified in their class reference documentation.
If you specify nil for the nibName parameter, you must either override the loadView method and create your views there or you must provide a nib file in your bundle whose name (without the .nib extension) matches the name of your view controller class. (In this latter case, the class name becomes the name stored in the nibName property.) If you do none of these, the view controller will be unable to load its view.
If a nib file is specified via the initWithNibName:bundle: method (which is declared by the superclass UIViewController), UITableViewController loads the table view archived in the nib file. Otherwise, it creates an unconfigured UITableView object with the correct dimensions and autoresize mask. You can access this view through the tableView property.