Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

One xib File with Multiple "File's Owner"s

I've got three different UITableViews, each in it's own view, accessed via tabs. All three tables would ideally share the same custom UITableViewCell class and .xib file.

I started with one table, setting the class of the .xib to my custom class and the File's Owner of the .xib to the table's parent UIViewController, which works great. All of the custom view-related code is in the cell's class (background images based on a property set by the controller, custom cell height based on the number of lines a label requires based on a cell property set by the controller, etc.).

The result is nice: the cell is responsible for all of the visual layout and responding to user actions on the cell's controls, while the view controller is responsible for creating the cells and setting their data.

Now that I need to reuse the cell in other tables, though, the fact that the custom cell's .xib has a single File's Owner is a problem. Rather than duplicating the .xib file, is there a simple way to allow multiple controllers to own it?

like image 906
Matthew Frederick Avatar asked May 01 '11 15:05

Matthew Frederick


People also ask

What is file owner in XIB?

The File owner is the object that loads the nib, i.e. that object which receives the message loadNibNamed: or initWithNibName: . If you want to access any objects in the nib after loading it, you can set an outlet in the file owner.

Which data format do XIB files use?

XIB files are in XML format to make them easier to use with version control systems and text-based tools. Let's go ahead and start using IB. To do this, first create an iOS App using the Single View Application iOS Project template in Xcode.

What are XIB files used for?

What are XIB files? From the point of view of the UI designer, XIB files contain the views or windows that you design in Interface Builder – the controls, their layout and properties, and their connections to your code. It is important to understand that on a technical level, XIB files are stored object graphs.

How do I copy an XIB file?

yes , you can copy the the contents of the xib file by just (command+c) and paste it into another xib file by using (command +v) but by simply cop and pasting you won't get the references you need to go the properties in xib file there you need to give the class name for which you have created a new xib file and then ...


3 Answers

A nib's File's Owner is not strictly enforced. Instead it is only used to determine available outlets and actions, and to set bindings within Interface Builder. You can load a nib with any object as its File's Owner regardless of the class set in the nib file. When a nib is loaded it will send messages to the File's Owner to re-establish bindings. If the actual File's Owner object does not recognize those selectors you will have triggered an "unrecognized selector" exception. This means that if your nib binds some UITableViewCell to the 'cell' outlet of its File's Owner then any object with a 'cell' property could load that nib. You just need to be careful not to use this behavior to send an unrecognized selector or unexpected outlet class.

In your case, consider creating a single UIViewController subclass to act as the File's Owner of your nib. Have each of your three existing controllers extend that view controller subclass. That way they can all inherit the same set of properties expected by the nib file and all safely load that nib while still defining their own custom behavior.

like image 146
Jonah Avatar answered Sep 17 '22 14:09

Jonah


The checked answer to this question discusses two approaches to load custom table cells from nib files that do not require to set the File's Owner to your specific controller. These approaches let you reuse cells with different owners.

like image 31
albertamg Avatar answered Sep 20 '22 14:09

albertamg


A "shared" super class as a file owner is not always a good solution. Remember that you can always load the xib in your view, and make connections without using outlet, for example:

UIView *aView = [[NSBundle mainBundle] loadNibNamed:@"MyXibFile" owner:self options:nil]
//Search subviews by tag. Obviously you need to set the tag on your view in MyXibFile
UILabel *aLabel = (UILabel*)[aView viewWithTag:996]; 
UILabel *aTextField = (UITextField*)[aView viewWithTag:997]; 
aTextField.delegate = self;
//etc...

I can't say that this is a clean solution, but in some cases could work better than inheritance .

like image 21
andreacipriani Avatar answered Sep 17 '22 14:09

andreacipriani