Loading the Different Nib Files

南笙酒味 提交于 2019-12-08 11:06:10

问题


I created two nib files for the iPhone and iPad, so my app will be universal.

I use this method to check if it is an iPad:

if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad)

but I don't know how to load the proper nib when it knows which it is.

Does anyone know the correct method to load to nib file, accordingly?


回答1:


Your interface files should be named differently so something like this should work.

UIViewController *someViewController = nil;
if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad)
{
    someViewController = [[UIViewController alloc] initWithNibName:@"SomeView_iPad" bundle:nil];
}
else
{
    someViewController = [[UIViewController alloc] initWithNibName:@"SomeView" bundle:nil];
}



回答2:


Actually, Apple does all this automatically, just name your NIB files:

MyViewController~iphone.xib // iPhone
MyViewController~ipad.xib // iPad

and load your view controller with the smallest amount of code:

[[MyViewController alloc] initWithNibName:nil bundle:nil]; // Apple will take care of everything



回答3:


You should use a initializer -[UIViewController initWithNibNamed:bundle:];. In your SomeViewController.m:

- (id)init {
    if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad) {
        if (nil != (self = [super initWithNibName:@"SomeViewControllerIPad"])) {
            [self setup];
        }
    } else {
        if (nil != (self = [super initWithNibName:@"SomeViewControllerIPhone"])) {
            [self setup];
        }
    }
    return self;
}


来源:https://stackoverflow.com/questions/6280442/loading-the-different-nib-files

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