How can i load the different xibs for single class depends on current device in iOS?

一笑奈何 提交于 2019-12-02 02:33:23

问题


I have the complete code in view controller. So, i need to display the same output in iPad,iPhone and iPod. So, am using single view controller for processing data.For this purpose how can i select the different XIB that may ipod or ipad depends on current device in iOS?

Instead of creatimg one more view controllers i want single viewcontroller and 2 XIB's


回答1:


if ([[UIDevice currentDevice] userInterfaceIdiom] == UIUserInterfaceIdiomPhone)
{
    self.viewController = [[[ViewController alloc] initWithNibName:@"ViewController_iPhone" bundle:nil] autorelease];
}
else
{
    self.viewController = [[[ViewController alloc] initWithNibName:@"ViewController_iPad" bundle:nil] autorelease];
}



回答2:


This is very simple when you create a universal app, itself it will give the code check the device type and load particular xib.

for example

 if ([[UIDevice currentDevice] userInterfaceIdiom] == UIUserInterfaceIdiomPhone) {
        self.viewController = [[[ViewController alloc] initWithNibName:@"ViewController_iPhone" bundle:nil] autorelease];
    } else {
        self.viewController = [[[ViewController alloc] initWithNibName:@"ViewController_iPad" bundle:nil] autorelease];
    }



回答3:


[UIDevice currentDevice] will give you the details about the current device. use this in a if loop and select the xib for iphone or i pad.

    if([UIDevice currentDevice].userInterfaceIdiom == UIUserInterfaceIdiomPad) 
{
 load 1 xib 
} 
else 
{ 
load another 
}


来源:https://stackoverflow.com/questions/14373739/how-can-i-load-the-different-xibs-for-single-class-depends-on-current-device-in

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