如何在设备类型上启动特定视图

时间:2012-12-08 16:23:48

标签: iphone ios ipad view

我正在尝试在启动时启动3个视图中的1个。我想要启动的视图取决于设备类型。以下是我目前在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];
    }
}

问题在于,当我启动应用程序时,会出现黑屏。

2 个答案:

答案 0 :(得分:2)

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

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

答案 1 :(得分:2)

有一种更清晰的方式来编写代码(包括修复):

- (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];