NSInternalInconsistencyException and UITableViewController

*** Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: '-[UITableViewController loadView] loaded the "LibraryViewController" nib but didn't get a UITableView.'

This is what I’m staring at right now, puzzled. I’ve already created a LOT of UIViewControllers and a LOT of UITableViews, and they’re always very straightforward.

  1. Define a new UIViewController’s .h and .m files.
  2. Create a nib file.
  3. Set the custom UIViewController to the File’s Owner.
  4. Add a view.
  5. Link the .view attribute from the UIViewController to the view.

Admittedly I don’t create many UITableViewControllers since they seem to do nothing that a normal UIViewController doesn’t do, but this is something of a learning experience right now.

Still, how complicated could it be to create a UITableViewController? This is supposed to be the easy way to create apps quickly, if you believe Apple.

So I created a UITableViewController and a .xib, set the UITableViewController as the File’s Owner in the xib, added a UITableView, set it as the view (curiously there’s no tableView outlet, but surely because it’s redundant, right???) and I should be good to go! What else could there be to do?

The UITableViewController class creates a controller object that manages a table view. It implements the following behavior:

  • 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.
  • If a nib file containing the table view is loaded, the data source and delegate become those objects defined in the nib file (if any). If no nib file is specified or if the nib file defines no data source or delegate, UITableViewController sets the data source and the delegate of the table view to self.

I’m doing everything Apple says to do, but it still doesn’t work! At runtime, I get the NSInternalInconsistencyException and it blows up.

I might never have solved this problem, and just dropped back to a UIViewController that implements UITableViewDelegate and UITableViewDataSource–like I always do–except I noticed this post on Stack Overflow.

Absurd as it may sound, apparently the answer is to create a nib file for your UITableViewController like such:

  1. Set File’s Owner to NSObject.
  2. Create a UITableView in the Interface Builder.
  3. Link the DataSource & Delegate to the File’s Owner in Interface Builder like you would normally.
  4. Don’t link File’s Owner to the UITableView.
  5. Implement -(void)loadView;
- (void)loadView {
    NSArray *views = [[NSBundle mainBundle] loadNibNamed:NSStringFromClass([self class]) owner:self options:0];
    self.view = views.lastObject;
}

After all of that, it will finally work.

Ugh. What a pain, and definitely not what Apple told me I’d have to do.

So I’m left wondering. What’s the point of UITableViewController? Apple makes it sound like they’re better in UIStoryBoards. Maybe I’ll continue this experiment in a further post.

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

w

Connecting to %s