How to use one xib with several controllers?

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

I want them all to use the same xib, so I start 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 a File Owner installed in MyViewController .

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

+6
source share
1 answer

You can download any XIB using

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

class NSBundle . FROM

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

you can load all XIB contents into an array. The order of the elements in the array is the same as you defined in Interface Builder without the owner of the file and the first responder.

+2
source

Source: https://habr.com/ru/post/896617/


All Articles