问题
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?
回答1:
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.
回答2:
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.
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
来源:https://stackoverflow.com/questions/4430573/single-view-controller-with-multiple-nibs