简单的问题如题,给个思路啊
一般是这样
1.建立两个视图控制器的类,一个给iphone 一个给ipad
2.在AppDelegate中 didFinishLauchingWithOption的时候判断设备是ipad还是iphone。什么设备就初始化什么类
官方文档中的说明:
bash
Nib 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
):
bash
YourViewControlle~iphone.xib YourViewControlle~ipad.xib
应用代码用统一的一行:
objc
YourViewController *vc = [[[YourViewController alloc] initWithNibName:nil bundle:nil] autorelease];
因为是参数都是空,在有ARC
的Objective-C
下,上面的代码可以写成:
objc
YourViewController *vc = [YourViewController new];
试试看~
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]; }
"建立两个视图控制器的类“ 我只要一个视图控制器的类。 两个XIB而已。 以前不知道,现在觉得不难,一个控制器的类直接关联两个XIB。很多变量都能共用了
initwith的时候判断一下呗