Tar_Tw45
Tar_Tw45

Reputation: 3222

Two UIViewController, one XIB File

Let's say, I have three UIViewController

So, NewUserFormViewController and UpdateUserFormViewController view controller inherit from it's parent to share the basic functionality. The different will be their method, create and update.

The views also have a lot of things in common, almost everything. The different view components between NewUserFormViewController and UpdateUserFormViewController is a button to perform save task (create or update)

Is it possible to have two UIViewController sharing one XIB file? Let's say, UserFormViewController.xib and then I do

[[NewUserFormViewController alloc] initWithNibName@"UserFormViewController" bundle:nil];
[[UpdateUserFormViewController alloc] initWithNibName@"UserFormViewController" bundle:nil];

The other question but important is, when I edit xib file with Interface Builder, what owner's reference outlets and IBActions is it talking about, NewUserFormViewController or UpdateUserFormViewController? (IBActions and Outlets showing when we right click at the Placeholders -> File's Owner)

If that's so, I will just use one XIB file and programmatically add other specific view component (It would be great to have only one XIB file so that I can make some changes at a place but effective on both)

Upvotes: 1

Views: 2114

Answers (2)

yixiang
yixiang

Reputation: 940

@Armaan I met the same problem by simply call subclass' alloc init method. I fixed this problem by using initWithNibName: and supply xib file name of parent class.

Upvotes: 1

Cyrille
Cyrille

Reputation: 25144

The "file owner" is just a convention so that XCode can show you the correct IBOutlets and IBActions in its inspectors. If you create a generic (in OO terms: abstract) UserFormViewController (.h, .m, .xib), wire it in IB; then subclass it in two NewUserFormViewController and UpdateUserFormViewController, they'll inherit their outlets and actions from their parent class without any problem.

Upvotes: 3

Related Questions