iPad的通用应用程序无法加载iPad .xib文件?

我一直在试图弄清楚为什么会出现这种情况,但似乎在我的通用应用程序的iPad版本中,它正在加载iPhone .xib,而不是iPad。

我用iphone.xib的后缀命名我的iPhone xib,并且用.xib离开了我的iPad。 我读过这样做,因为有人说,为他们工作,但在我的情况下,它不适合我!

即使我做〜ipad.xib和〜iphone.xib为不同的.xib文件,它仍然加载iPhone版本!

* 有什么办法可以完全确认它是加载iPhone版本而不是iPad版本?

有没有办法解决这个问题,以便iPad加载iPad .xibs?**

谢谢!

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions { self.window = [[[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]] autorelease]; // Override point for customization after application launch. self.viewController = [[[MyViewController alloc] initWithNibName:@"MyViewController" bundle:[NSBundle mainBundle]] autorelease]; self.window.rootViewController = self.viewController; [self.window makeKeyAndVisible]; return YES; } 

在我的应用程序中,我这样做。 我为iPad的视图创build单独的.h,.m和XIB文件,然后在AppDelegate中,我只是做一个if条件,它决定它将显示哪个视图控制器。
顺便说一句。 我不在XIB上使用这些后缀,我将它们命名为我想要的。

我的AppDelegate.h文件(它的一部分)

  @class FirstViewController; @class FirstIpadViewController; ....... ....... @property (nonatomic, retain) IBOutlet FirstViewController *viewController; @property (nonatomic, retain) IBOutlet FirstIpadViewController *ipadViewController; 

我的AppDelegate.m文件(它的一部分)

 if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPhone) { self.window.rootViewController = self.viewController; [self.window makeKeyAndVisible]; } else { self.window.rootViewController = self.ipadViewController; [self.window makeKeyAndVisible]; } 

这绝对应该做到这一点。 只需要将.h文件中的类和属性更改为你的视图控制器,你应该好好去:)

编辑

我刚刚发现如何做到这一点。 而正确的命名约定是_iPhone和iPad。 这与我上面发布的基本相同,只是变化是它将具有相同的.h和.m文件,但是不同的XIB。

在AppDelegate .m文件中

  - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions { self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]]; // Override point for customization after application launch. if ([[UIDevice currentDevice] userInterfaceIdiom] == UIUserInterfaceIdiomPhone) { self.viewController = [[ViewController alloc] initWithNibName:@"ViewController_iPhone" bundle:nil]; } else { self.viewController = [[ViewController alloc] initWithNibName:@"ViewController_iPad" bundle:nil]; } self.window.rootViewController = self.viewController; [self.window makeKeyAndVisible]; return YES; } 

这个解决scheme在使用自己定制的xibs的情况下与Storyboards一起工作。 它重新使用与视图相同的.h和.m文件:

  • ViewController.h
  • ViewController.m
  • ViewController.xib
  • 视图控制器〜iPad.xib

在您的ViewController.m文件中添加:

 - (id)initWithCoder:(NSCoder *)aDecoder { if (self) { NSString* nibName = NSStringFromClass([self class]); if ([[UIDevice currentDevice] userInterfaceIdiom] == UIUserInterfaceIdiomPhone) { self = [super initWithNibName:nibName bundle:nil]; } else if ([[UIDevice currentDevice] userInterfaceIdiom] == UIUserInterfaceIdiomPad) { self = [super initWithNibName:[nibName stringByAppendingString:@"~iPad"] bundle:nil]; } return self; } return self; } 

只要检查xibs文件的所有者视图控制器自定义类名称集和视图出口设置。