0

我正在尝试在启动时启动 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];
    }
}

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

4

2 回答 2

2

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

self.window.rootViewController = self.viewController;
[self.window makeKeyAndVisible];
于 2012-12-08T16:26:22.907 回答
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];
于 2012-12-08T16:43:02.853 回答