如何启动设备types的特定视图

我正尝试在启动时启动3个视图中的1个。 我想要启动的视图取决于设备types。 这是我到目前为止在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 && [UIScreen mainScreen].bounds.size.height == 568.0) { self.viewController = [[ViewController alloc] initWithNibName:@"ViewController_Portrait5" bundle:nil]; } else if ([[UIDevice currentDevice] userInterfaceIdiom] == UIUserInterfaceIdiomPhone && [UIScreen mainScreen].bounds.size.height == 480.0) { self.viewController = [[ViewController alloc] initWithNibName:@"ViewController_Portrait4" bundle:nil]; } else { self.viewController = [[ViewController alloc] initWithNibName:@"ViewController_PortraitPad" bundle:nil]; } } 

问题是,当我启动应用程序,有一个黑屏。

您不在窗口上设置控制器,在方法结束时,添加:

 self.window.rootViewController = self.viewController; [self.window makeKeyAndVisible]; 

编写代码有更简洁的方法(包括修复):

 - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions { self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]]; NSString *nibName; if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad) { nibName = @"ViewController_PortraitPad"; } else { if ([UIScreen mainScreen].bounds.size.height == 480.0) { nibName = @"ViewController_Portrait4"; } else { nibName = @"ViewController_Portrait5"; } } self.viewController = [[ViewController alloc] initWithNibName:nibName bundle:nil]; self.window.rootViewController = self.viewController; [self.window makeKeyAndVisible];