8

I am trying to construct a view controller that can be 'skinned' -- that is, have multiple appearances or personalities but that uses a single controller. Each view will have the same buttons, etc, but I would like to be able to load each nib file (skin) into the same view controller. I can create multiple nib files, but I don't see how to connect the buttons, and actions. Can I specify the same 'file's owner' for multiple nib files? (HOW?).

Can this be done?

Billy Pilgrim
  • 1,842
  • 3
  • 22
  • 32

2 Answers2

8

This is totally possible. Just create new nib files and in Interface Builder set the file owner to the class. You can then hook up your outlets and actions just like before. From your code just specify the correct nib file in the initWithNibName: method.

If the only changes are cosmetic, you might be better off just making those changes in code, but your proposed method will work just fine.

theChrisKent
  • 15,029
  • 3
  • 61
  • 62
  • Thanks! (stupid follow up question: how do I set the file owner?) – Billy Pilgrim Dec 13 '10 at 16:19
  • There is a video here: http://vimeo.com/5105309 that shows how to do it in the first 25 seconds or so. Basically, using xcode, add a new nib file to your project and double click it to open it in Interface Builder. Select the File Owner object (Yellowish Cube) then open the Identity Inspector (Tools > Identity Inspector) and in the "Class Identity" Section set the Class to your View Controller Class. Then you'll be able to hook up your outlets and actions just like before. – theChrisKent Dec 13 '10 at 20:03
0

you can do it much easier if you literally copy and paste the view inside the nib file into the same nib file, so that you have 2 separate views inside 1 nib file.

example pic

then you can swap between the views as you load the nib like so:

NSArray *temp = [[NSBundle mainBundle] loadNibNamed:@"Widget" owner:self options:nil];
Widget *w = [temp objectAtIndex:0]]; // or 1 or 2 etc to get the different views

this will copy all your button connections etc, so you can just fiddle around with the copy without having to setup everything again

Fonix
  • 11,447
  • 3
  • 45
  • 74