JDev
JDev

Reputation: 5558

How to Launch Specific Views on Device Type

I am trying to launch 1 of 3 views on startup. The view I want to launch is depending on the device type. Here is what I have so far in the 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];
    }
}

The problem is that, when I launch the app, there is a black screen.

Upvotes: 0

Views: 63

Answers (2)

rmaddy
rmaddy

Reputation: 318824

There's a much cleaner way to write that code (including the fix):

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

Upvotes: 2

Andy Obusek
Andy Obusek

Reputation: 12842

You aren't setting the controller on the window, at the end of the method, add:

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

Upvotes: 2

Related Questions