问题
I have an UIVIewController "controller1" for instance. This controller is instanciated with initWithNibName... with "file1.xib". I want to dynamically change the xib file of my "controller1" to "file2.xib"
To resume :
"controller1" <-> "file1.xib" and I want to dynamically have : "controler1" <-> "file2.xib"
How can I do this ?
Hope I was clear.
回答1:
When you want to change views in a UIViewController just just use this code:
NSArray *nibObjs = [[NSBundle mainBundle] loadNibNamed:@"file2" owner:self options:nil];
UIView *aView = [nibObjs objectAtIndex:0];
self.view = aView;
回答2:
I have two responses :
1) Why - this isn't something that you would normally do - what makes this case special? In fact, I'm going to edit this to be more emphatic --> DON'T DO IT <-- there will be all sorts of dependencies in UIViewController that you don't know about (for example if there is a low memory warning and your view controller unloads it's view, which xib would it load the view from when it had to display it again?)
2) If you desperately had to you can remove the view and reload it though NSBundle's loadNibNamed:owner: passing in the new xib and self as the owner.
来源:https://stackoverflow.com/questions/4498822/how-to-change-dynamically-the-xib-of-a-uiviewcontroller