views:

2845

answers:

6

I was trying to follow the Table View Programming Guide for iPhone OS but was having trouble creating a new Table View Controller that loads its data from a nib file.

Specifically, I was trying to follow the steps in this part:

If you prefer to load the table view managed by a custom table-view controller from a nib file, you must do the following:

  1. In Interface Builder, create an empty Cocoa Touch nib file (File > New).
  2. Drag a UITableViewController object from the Interface Builder Library into the nib document window.
  3. Save the nib file in your project directory under an appropriate name and, when prompted, select your project to have the nib file added to it.
  4. Select Table View Controller in the nib document window and open the Identity pane of the inspector. Set the class to your custom table-view controller class.
  5. Select File’s Owner in the nib document window and set its class identity to the custom table-view controller class.
  6. Customize the table view in Interface Builder.
  7. Select the table-view controller in the nib document window, open the Attributes pane of the inspector, and enter (or select) the name of the nib file in the Nib Name field.

So I created a new UITableViewController subclass in Xcode (called "MyTableViewController"), then I went into IB and followed those steps. I made sure to link up all the Class attributes to the same name as the UITableViewController subclass I made in Xcode like it says in the steps.

But now I get the following warning in IB:

"My Table View Controller" has both its "View" and "Nib Name" properties set. This configuration is not supported.

When I run the application and push the table view controller, it appears but it seems like nothing is being loaded from the nib file at all (e.g. I set the alpha to 0 instead of 1).

Any idea as to what I'm doing wrong?

Thanks for the help.


Here's some more information that might help you understand the situation better.

I noticed a few differences between creating a UITableViewController with the template (e.g. by creating a new Navigation-based Application) vs. creating one yourself (e.g. following the steps above). I'm going to refer to each as TemplateNib and CustomNib, respectively, to make it easier to understand the differences.

In TemplateNib, it has the following objects in the document window:

  • File's Owner
  • First Responder
  • Table View

In CustomNib, it has the following objects in the document window:

  • File's Owner
  • First Responder
  • My Custom Table View Controller
    • Table View

Another difference is in the File's Owner links...

TemplateNib's File's Owner:

  • Outlets
  • tableView -> Table View
  • view -> Table View
  • Referencing Outlets
  • dataSource -> Table View
  • delegate -> Table View

CustomNib File's Owner:

  • Outlets
  • view -> (nothing)

CustomNib My Table View Controller:

  • Outlets
  • view -> Table View (this is grayed out so you can't delete it)
  • Referencing Outlets
  • dataSource -> Table View
  • delegate -> Table View


Update:

I tried to mimic the .xib file that is created by the template by following these steps:

  1. Created an empty file in Interface Builder.
  2. Set the File's Owner to the class that inherits from UITableViewController.
  3. Added a Table View to the document window.
  4. Set the Table View's dataSource and delegate to File's Owner.
  5. Set the File's Owner view to the Table View.
  6. Added a tableView propery in the Identity pane of type UITableView.
  7. Set the File's Owner tableView property (which I just created) to the Table View.

However, this still seems like it is not loading it from the NIB file. (I also never set the name of the NIB file anywhere though... is there anyplace I need to set it or does it look for one with the same name?).

I then tried overriding initWithNibName to load from the name of the nib file, and now it does seem to load it from the nib file. However, if I look at the .m file of the TemplateNib table view controller, it doesn't need to override this method, why is that? I still think I am doing it the wrong way cause the Programming Guide didn't mention anything about doing it this way.


Update:

I tried comparing the two .xib files using a diff tool, the only significant difference between the two seems to be:

<string key="superclassName">UITableViewController</string>
// and:
<reference key="NSSuperview"/>

I don't see any reference to the Nib file in the original file anywhere, are there any other files I should check?


Update:

It seems like the thing that makes TemplateNib load from the nib file is that in the MainWindow.xib (default name given by the template), the RootViewController is added with the NIB Name property having the value "RootViewController". Additionally, its class is set to "RootViewController".

I tried putting a breakpoint in both initWithNibName:bundle: and initWithStyle: on the RootViewController, however, it never gets to there. I'm kinda wondering how the TableViewController is created when you set it up in the MainWindow.xib like that.

I also tried adding the my custom table view controller to MainWindow.xib, setting the class and nib names in hopes that it will load it from the nib file I specified, but it doesn't even call iniWithNibName.

A: 

Instead of doing all that, I would use the "New File" iPhone UI template to create a TableViewController with xib file option checked. Then you get a controller and xib file all wired together properly.

Kendall Helmstetter Gelner
I don't see that option for UITableViewController... I only see it for UIViewController. That would be a much easier solution though.
Senseful
A: 

Eagle, when you create a new file, select the "UIViewController subclass" icon. There's a checkbox to make it a UITableViewController subclass just above the checkbox to include XIB file.

No Surprises
I don't see that option, I'm running Xcode 3.1.3.
Senseful
A: 

I have just run into the same bug. Did anybody figure out a way to repair the file without recreating it?

William
A: 
  1. Create a TableViewController in Xcode.
  2. Create an empty nib file in Interface Builder.
  3. Set the File's Owner Class property to the TableViewController from step 1.
  4. Add a TableView to the empty nib file.
  5. Set the File's Owner view property to the TableView from step 4.
  6. Customize the TableView in IB as you want.
  7. Override the initWithNibName:bundle: method in Xcode for the TableViewController you created and use code similar to the following:


- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil {
    if (self = [super initWithNibName:@"MyNibName" bundle:nibBundleOrNil]) {
        // Custom initialization
    }
    return self;
}
Senseful
I couldn't figure out the proper way to do this, without having to reference the nib name in Xcode, but for now this is the best method I found. If someone finds a better way, I will update the accepted answer.
Senseful
A: 

You've got two places where your UITableViewController shows up in Interface Builder.

(1) It shows up in the nib with the controllers own name.

(2) It shows up as a controller object in the nib of another object, usually the MainWindow.

Your problem is at (2). There are two ways to set the tableview for a UITableViewController in Interface builder. First, you can create a UITableView under the controller in the MainWindow and connect that to the controller's view property. Secondly, you can bring up the inspector in the attributes pane and in the popmenu listed "NibName" select the name of the controllers nib.

You can't use both systems at once because the first loads a view from the MainWindow nib file and second loads a completely unrelated view from the controller's separate nib file.

This is one of those maddening errors that using Interface Builder makes so hard to track down.

TechZen
+1  A: 

I had this same problem last night and found this post while trying to search for the answer. I ended up solving it.

Basically I had opened up the wrong XIB file (I hoped main_window.xib, not the view controllers xib)

I cut all the controls from my main xib, pasted them into the controllers xib, realigned everything, reconnected all the outlets/actions and the warning went away :)

Hope this helps someone :)

Matt