5 个回答
if ([[UIDevice currentDevice] userInterfaceIdiom] == UIUserInterfaceIdiomPhone) {
YourViewController *vc = [[[YourViewController alloc] initWithNibName:@"YourViewController_iPhone" bundle:nil] autorelease];
}
else
{
YourViewController *vc = [[[YourViewController alloc] initWithNibName:@"YourViewController_iPad" bundle:nil] autorelease];
}

initwith的时候判断一下呗

一般是这样
1.建立两个视图控制器的类,一个给iphone 一个给ipad
2.在AppDelegate中 didFinishLauchingWithOption的时候判断设备是ipad还是iphone。什么设备就初始化什么类

"建立两个视图控制器的类“ 我只要一个视图控制器的类。 两个XIB而已。 以前不知道,现在觉得不难,一个控制器的类直接关联两个XIB。很多变量都能共用了

官方文档中的说明:

bashNib names that include a platform-specific identifier such as ~iphone or ~ipad are loaded only on a device of the corresponding type. For example, a nib name of MyViewController~ipad.nib is loaded only on iPad. If your app supports both platform types, you must provide versions of your nib files for each platform.

详见: UIViewController/nibName


即准备2个XIB文件,文件名中使用不同的后缀(~iphone/~ipad):

bashYourViewControlle~iphone.xib
YourViewControlle~ipad.xib

应用代码用统一的一行:

objcYourViewController *vc = [[[YourViewController alloc] initWithNibName:nil bundle:nil] autorelease];

因为是参数都是空,在有ARCObjective-C下,上面的代码可以写成:

objcYourViewController *vc = [YourViewController new];

试试看~

撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进
推荐问题