Lukas
Lukas

Reputation: 2535

Multiple controllers use one xib

tried searching for something similar on the net but no luck. I have many different view controllers, but everyone of them have a tableview in it. So my question is: Can i have one xib file with a tableview, that would be used by all of these controllers? If yes an example of how to do it would be super! Thanks in advance!

Upvotes: 2

Views: 911

Answers (1)

Andrew
Andrew

Reputation: 3241

Yes, you can. You can instantiate the xib's top level objects with:

NSArray *topLevelObjects = [[UINib nibWithNibName:@"YourNibNameHere" bundle:nil] instantiateWithOwner:self options:nil]

This will get you all the objects (topmost views and/or gesture recognizers, etc) from that xib, then you find the one that you need (or if there's just one, it'll be the only one) and add it as a subview.

UIView *view = [topLevelObjects lastObject];
[self.view addSubview:view];

Upvotes: 1

Related Questions