How to change dynamically the xib of a UIVIewController

别来无恙 提交于 2019-12-13 12:33:48

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!