Search code examples
iphoneipaduiviewcontrollermodalviewcontrolleruiwindow

Presenting Modal View Controller before window is visible


I would like to present a view controller modally before calling -makeKeyAndVisible on the application's window. However, this code only shows the mainNav view controller:

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
    MainViewController *main = [[MainViewController alloc] init];
    UINavigationController *mainNav = [[UINavigationController alloc] initWithRootViewController:main];

    if ([[NSUserDefaults standardUserDefaults] boolForKey:@"Restore"]) 
    {
        DetailViewController *detail = [[DetailViewController alloc] init];
        UINavigationController *detailNav = [[UINavigationController alloc] initWithRootViewController:detail];

        // changing main to mainNav does not help
        [main presentModalViewController:learnNav animated:NO];

        [detailNav release]; [detail release];

    }

    self.window.rootViewController = mainNav;

    [main release]; [mainNav release];

    [self.window makeKeyAndVisible];

    return YES;
}

What am I missing?


Solution

  • You should better make the window appear and then present the modal view with animated=NO. What's the point of presenting the modal view before everything else is instantiated and displayed?

    Edit

    To try to make your code work, here are a couple of hints. Try this:

    [mainNav presentModalViewController:learnNav animated:NO];
    

    or this:

    [main.navigationController presentModalViewController:learnNav animated:NO];
    

    I'd say that these two methods work best if they're put after the makeKeyAndVisible call.