I have an iPhone/iPad app screen which contains two possible layouts that I have defined in two interface builder files (xib/nib). When the app starts, I look in the userdefaults and choose which one to load up.

However if the user chooses the settings button and loads the in app settings menu and changes the layout, then hits the navigation button to go back to the previous screen, how would I change the user interface/xib file to be the new choice without restarting the app?

Thank you.

有帮助吗?

解决方案

You can force the XIB to load using:

[[NSBundle mainBundle] loadNibNamed:@"XIBName" owner:self options:nil];

Edit

Try the following:

Add the following in an utility class which you can use anywhere.

+ (id)loadNibNamed:(NSString *)nibName ofClass:(Class)objClass {
    if (nibName && objClass) {
        NSArray *objects = [[NSBundle mainBundle] loadNibNamed:nibName 
                                                         owner:nil 
                                                       options:nil];            
        for (id currentObject in objects ){
            if ([currentObject isKindOfClass:objClass])
                return currentObject;
        }
    }

    return nil;
}

Then make the call like this:

MyClass *myClassInstance = [Utility loadNibNamed:@"the_nib_name" 
                                         ofClass:[MyClass class]];

See this also: Single View controller with multiple nibs?

其他提示

MyUIViewController *controller;
if (IS_IPAD){
  controller = [[MyUIViewController alloc] initWithNibName:@"MyUIViewController_iPad" bundle:nil];
} else {
  controller = [[MyUIViewController alloc] initWithNibName:@"MyUIViewController_iPhone" bundle:nil];
}

And you can for example push from a navigationController

[self.navigationController pushViewController:controller animated:YES];
许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top