https://segmentfault.com/q/1010000000128506
1个viewController怎么关联2个XIB,一个是iphone的XIB,一个是ipad的xib
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];
}
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.
即准备2个XIB文件,文件名中使用不同的后缀(~iphone/~ipad):
YourViewControlle~iphone.xib
YourViewControlle~ipad.xib
应用代码用统一的一行:
YourViewController *vc = [[[YourViewController alloc] initWithNibName:nil bundle:nil] autorelease];
因为是参数都是空,在有ARC的Objective-C下,上面的代码可以写成:
YourViewController *vc = [YourViewController new];
试试看~
本文介绍了一种在iOS开发中针对不同设备(iPhone与iPad)使用不同XIB文件的方法。通过条件判断加载特定平台的XIB,确保界面适配。同时介绍了如何利用XIB文件名中的特殊标识符简化代码。
118

被折叠的 条评论
为什么被折叠?



