I have a subclass of UIViewController:
@interface KBViewController : UIViewController
with multiple xibs, for example, one is a Qwerty and the another is Dvorak layout:
KBViewControllerQuerty~iphone.xib
KBViewControllerDvorak~iphone.xib
So when the user click a button, Qwerty is switch to Dvorak. As you may see, the code logic is identical for both keyboard layouts. What I need is to reload the view with another xib.
Hopefully, all the buttons in the Dvorak xib will be hooks to the responding IBOutlets in KBViewController.
What is the right way to switch between the two xibs?
All Nibs has the designated
File's Owner. IBOutlet and IBAction linking is done based upon theFile's Owner. So you could define a view controller and two Nibs, with each Nib file’sFile's Ownerset to the defined view controller.That is, if you set
File's Ownerof all KBViewController*.xib files to KBViewController and have a KBViewController object somewhere you could load the KBViewController*.xib you want byinitWithNibNamedmethod (recreate the view controller)If you should maintain the same KBViewController object all along, you could create a KBViewController object without Nib. In KBViewController.m, implement
loadViewand manually load the UIView object with-[NSBundle loadNibNamed]method (load and change self.view programmatically).Note
owner:selfin above code. It must match withFile's Ownerof the @”SomeNibFile”.To change already loaded view:
More detailed explanation:
Resource Programming Guide – Loading Nib Files Programmatically