Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I use one xib with multiple view controllers?

In my program, I have a UIViewController subclass MyViewController and two subclasses of that view controller.

I want them all to use the same xib so I initiate them as

SubClass *SC = [[SubClass alloc] initWithNibName:@"MyViewController" bundle:nil];
[self presentModalViewController:SC animated:NO];
[SC release];

SubClass is a subclass of MyViewController, which is a subclass of UIViewController. In MyViewController.xib, I have File's Owner set to MyViewController.

If I only was going to have two subclasses, I would probably just duplicate the xib but I plan to have many, many subclasses, all using the same xib.

like image 741
WolfLink Avatar asked Sep 05 '11 04:09

WolfLink


People also ask

What's the difference between storyboard and xib?

XIB and Storyboard are used for creating interfaces for users. One important point is,xibs are used for creating a single view(it has single file owner at the top of the xib file), but in-case for viewcontroller, multiple screens can be added and its flow can also be monitored(it has separate file owners).

How do you duplicate xib?

The best way to do this is use cmd ⌘ ,shift, s on the xib or storyboard. Then save it with a new name or location. This is the easiest way to do it, and also the safest, I reckon. Note this is the Duplicate option under the File menu.


1 Answers

You can load any XIB with

- (NSArray *)loadNibNamed:(NSString *)name owner:(id)owner options:(NSDictionary *)options

of the NSBundle class. With

NSArray *arr = [[NSBundle mainBundle] loadNibNamed:@"foo" owner:nil options:nil]; 

you can load all contents of a XIB into an array. The order of the items in the array is the same you defined in Interface Builder without File's Owner and First Responder.

like image 198
tomk Avatar answered Oct 02 '22 11:10

tomk