如何为单个类加载不同的xib取决于iOS中的当前设备?

时间:2013-01-17 07:01:01

标签: iphone ios ipad uiviewcontroller xib

我在视图控制器中有完整的代码。所以,我需要在iPad,iPhone和iPod上显示相同的输出。因此,我使用单视图控制器来处理数据。为此目的,我如何选择可能ipod或ipad取决于iOS中当前设备的不同XIB?

我想要一个视图控制器和2个XIB的

,而不是另外创建一个视图控制器

3 个答案:

答案 0 :(得分:1)

创建通用应用程序时非常简单,它本身会让代码检查设备类型并加载特定的xib。

例如

 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];
    }

答案 1 :(得分:0)

[UIDevice currentDevice]会为您提供有关当前设备的详细信息。在if循环中使用它,并为iphone或i pad选择xib。

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

答案 2 :(得分:0)

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];
}
相关问题