iOS Development: Proper Use of initWithNibName:bundle: Affects UITableViewController

时间:2024-08-03 12:37:20

Address:http://www.outofcore.com/2011/07/ios-development-proper-use-of-initwithnibnamebundle-affects-uitableviewcontroller/

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 nil to initWithNibName:bundle: like this:

1
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.

1
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 toinitWithNibName: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:

1
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.

UIViewController

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.

UITableViewController

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.

iOS Development: Proper Use of initWithNibName:bundle: Affects UITableViewControllerAbout Louis Feng

I have been a computer graphics enthusiast and researcher for many years. My interests has broadened to include mobile, high performance computing, machine learning, and computer vision.