Search code examples
iphoneuitableviewuisearchbarxibuisearchdisplaycontroller

NIB File Doesn't Display at Runtime


I am pulling my hair out on this one. I have a NavigationController with two levels of TableViews. Each TableView is in its own NIB file. The first level simply displays a list. Upon selecting a cell, it takes the user to a second level TableView with a more detailed list. It is on this second level TableView that I want to display a search bar (actually I am using a SearchDisplayController as well). I have added it to the TableView because I want the SearchBar to scroll with the table.

Below, I am displaying two screenshots. The first is the second level tableview in InterfaceBuidler. The second is the second level tableview at runtime. For some reason, the SearchBar doesn't display at runtime.

I have tried creating a completely new project from scratch and the same things happens. I don't understand why the SearchBar doesn't display on a NIB pushed on the NavigationController.

Before you ask, if I put the SearchBar on the first level TableView, it shows up just fine. Yes, I am adding it to the TableView itself, so it is a part of the view that should be displayed.

Help! What am I doing wrong?

XIB File

This is what actually displays after the XIB is pushed...

SearchBar Doesn't Display?


Solution

  • Okay, I'm going to have to answer my own question. I thought about deleting it, but perhaps this could help someone else. I really pulled my hair out on this one. It wasn't because of anything I was doing wrong so much as a fundamental misunderstanding of how iPhone development works.

    In a nutshell, the problem was with this line of code, which instantiated the TableViewController:

    self.downloadDetailViewController = [[DownloadDetailTableViewController alloc] initWithStyle:UITableViewStyleGrouped];
    

    When you use XCode to create a new class, you can use the following:

    "Add -> New File -> Cocoa Touch Class -> UIViewController subclass -> UITableViewController subclass AND With XIB for user interface"

    The problem is that the NIB has absolutely nothing to do with the UITableViewController until you tell your code to use it. To me, this seems like a bug in XCode or at the very least something that is counterintuitive. When the development environment creates all three files together, it would only make sense that they would work together, but they don't.

    Instead, the solution is to modify the line of code as follows:

    self.downloadDetailViewController = [[DownloadDetailTableViewController alloc] initWithNibName:@"SecondaryView" bundle:[NSBundle mainBundle]];
    

    Hopefully this can help someone else...