Creating a nib file for a table view controller

I tried to follow the Table Programming Guide for iPhone OS , but there was a problem creating a new table view controller that loads its data from the nib file.

In particular, I tried to follow the steps in this part :

If you prefer to load a table view controlled by a custom table view controller from the nib file, you must follow these steps:

  • In the Builder interface, create an empty Cocoa Touch nib file (File> New).
  • Drag the UITableViewController object from the Builder Library interface in the nib document window.
  • Save the nib file in the project directory under the appropriate name and when prompted, select the project so that the nib file is added to it.
  • Select Table View Controller in the nib document window and open the Inspector Identification Panel. Define a class for your custom table view controller class.
  • Select "File Owner" in the nib document window and set its class to the identity of the user table view controller class.
  • Customize table view in Interface Builder.
  • Select the table view controller in the nib document window, open the Attributes panel inspector and enter (or select) the name of the tip file in the Thread name field.

So, I created a new subclass of UITableViewController in Xcode (called "MyTableViewController"), then I went to IB and followed these steps. I have bound all the Class attributes with the same name as the UITableViewController subclass created in Xcode, as it says in the steps.

But now I get the following warning in IB:

“My Table View Controller” has both its “View” and “Thread Name” properties set. This configuration is not supported.

When I launch the application and press the table view controller, it appears, but it seems that nothing is being loaded from the nib file at all (for example, I set alpha to 0 instead of 1).

Any idea on what I'm doing wrong?

Thanks for the help.


Here is more information to help you better understand the situation.

I noticed a few differences between creating a UITableViewController with a template (for example, creating a new application based on navigation) and creating it (for example, following the steps above). I will refer to each as TemplateNib and CustomNib, respectively, to simplify the understanding of the differences.

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

  • File owner
  • First responder
  • View table

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

  • File owner
  • First responder
  • My Custom Table View Controller
    • View table

Another difference in File Owner links ...

TemplateNib file owner:

  • Outlets
  • tableView → View Table
  • view → View table
  • Relay outputs
  • dataSource -> View table
  • delegate -> View table

Custom CustomNib File:

  • Outlets
  • view → (nothing)

CustomNib My Table View Controller:

  • Outlets
  • view → Table View (displayed in gray, so you cannot delete it)
  • Relay outputs
  • dataSource -> View table
  • delegate -> View table

Update:

I tried to simulate the .xib file created by the template by following these steps:

  • Created an empty file in Interface Builder.
  • Set the owner of the file to a class that inherits from the UITableViewController.
  • A table view has been added to the document window.
  • Set the DataSource view of the table and delegate it to the owner of the file.
  • Set the File Owner view to the table view.
  • Added the tableView property in the Identity panel of the UITableView type.
  • Set the TableView property of the file owner (which I just created) in the table view.

However, it still seems that it is not loading it from the NIB file. (I also never set the NIB file name anywhere, though ... is there somewhere where I need to install it, or is it looking for the same with the same name?).

Then I tried to override initWithNibName to load from the nib file name, and now it seems to load it from the nib file. However, if I look at the .m file of the TemplateNib table view controller file, it does not need to override this method, why? I still think I'm doing it wrong, because the programming guide says nothing about how to do this.


Update:

I tried comparing two .xib files using the diff tool, the only significant difference between them:

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

I do not see the links to the Nib file in the source file anywhere, are there any other files that I should check?


Update:

It seems that loading TemplateNib from the nib file is that in MainWindow.xib (the default name specified by the template), the RootViewController is added with the name NIB Name set to "RootViewController". In addition, its class is set to "RootViewController".

I tried to set a breakpoint as in initWithNibName: bundle: and initWithStyle: on the RootViewController, however, it never gets there. I am wondering how the TableViewController is created when configured in MainWindow.xib.

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

+4
source share
5 answers
  • Create a TableViewController in Xcode.
  • Create an empty nib file in the Builder interface.
  • Set the Class File Owner Class property for the TableViewController element from step 1.
  • Add TableView to an empty nib file.
  • Set the View File Owner View property to the TableView from step 4.
  • Set up TableView in IB as you want.
  • Override the initWithNibName: bundle: method in Xcode for the created TableViewController 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; } 
+7
source

I had the same problem last night and I found this post trying to find the answer. I decided to solve it.

I basically opened the wrong XIB file (I was hoping that main_window.xib, not xib view controllers)

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

Hope this helps someone :)

+2
source

Eagle, when you create a new file, select the "UIViewController subclass" icon. Check the box to make it a subclass of the UITableViewController just above the box to include the XIB file.

+1
source

You have two places where your UITableViewController appears in the Builder interface.

(1) It is displayed at the tip with the controller's own name.

(2) It appears as a controller object at the tip of another object, usually MainWindow.

Your problem is in (2). There are two ways to set a tableview for a UITableViewController in an interface builder. First, you can create a UITableView under the controller in MainWindow and connect it to the controller's view property. Secondly, you can call the inspector in the attribute panel, and in the "NibName" list, select the name of the controller.

You cannot use both systems at once, because the first loads the view from the nib file of MainWindow, and the second loads the completely unrelated view from a separate controller nib file.

This is one of those crazy bugs that are so hard to track with Interface Builder.

+1
source

Instead of doing all this, I would use the iPhone's New File template to create a TableViewController with an xib parameter. Then you get the controller and xib file, all wired together correctly.

0
source

All Articles