如何为单个类加载不同的xib?

时间:2013-01-17 10:02:28

标签: iphone ios ipad xib viewcontroller

  

可能重复:
  How can i load the different xibs for single class depends on current device in iOS?

我开发了一个通用app.i实现了iPhone的应用程序。现在我必须为iPad实现。我有关于数据处理的完整代码在视图控制器和view-controller.xib用于iPhone。我该如何使用iPad的代码。

我为ipad创建了一个xib文件(ViewController_iPad.xib)。当我在iPad中运行应用程序时,我正在获取ipad xib,但数据没有显示。

如何获取viewController中的数据?

先谢谢。

3 个答案:

答案 0 :(得分:1)

在app delegate

中添加此方法
 +(NSString *)getNibName:(NSString *)strNibName
    {
        if(UI_USER_INTERFACE_IDIOM()==UIUserInterfaceIdiomPad)
            return strNibName;
        else
            NSLog(@"%@",[NSString stringWithFormat:@"%@_iphone",strNibName]);
            return [NSString stringWithFormat:@"%@_iphone",strNibName];
    }

并在需要时调用此方法

    ViewController *obj=[[ViewController alloc] initWithNibName:[AppDelegate getNibName:@"ViewController"] bundle:nil];
[self.navigationController pushViewController:obj animated:YES];

答案 1 :(得分:1)

iOS有一种简单的方法可以为iPad和iPhone加载不同的NIB

如果您将NIB文件命名为:ViewController~ipad.xibViewController~iphone.xib iOS将加载相应的文件。

我只将~ipad视图添加到我的通用项目中,没有任何设备说明符的NIB将被加载到所有其他设备上。

您现在可以加载UIViewController,例如:

 [[UIViewController alloc] alloc] initWithNibName:@"ViewController" bundle:nil];

这也适用于图像:

back.png //Normal images
back@2x.png // Retina images
back~ipad.png // iPad normal image
back@2x~ipad.png // Retina iPad images

答案 2 :(得分:0)

首先,您必须检查iPad xib中的文件所有者应该是与iPhone xib相同的控制器。然后在iPhone xib组件中进行所有连接,然后这应该可以。