问题
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