Search code examples
iphoneuiviewcontrolleruiapplicationdelegate

How do I present a modal view at application startup?


I have the following code in application didFinishLaunchingWithOptions where I want to present a modal view controller for user login.

LoginViewController_iPhone *loginViewController=[[LoginViewController_iPhone alloc]initWithNibName:@"LoginViewController_iPhone" bundle:nil];
UINavigationController *loginNavigationController=[[UINavigationController alloc]initWithRootViewController:loginViewController];
loginNavigationController.modalPresentationStyle=UIModalPresentationFullScreen;
[self.window.rootViewController presentModalViewController:loginNavigationController animated:NO];

[loginViewController release];
[loginNavigationController release];

However, all I get is a blank white screen. If I substitute the following

self.window.rootViewController=loginNavigationController;

the login screen displays correctly. There is no other view controller assigned to the rootViewController property as the app is just starting. Do I need another view controller assigned to get this to work?


Solution

  • Yes. you need to assign something to the window's rootViewController property in order to call its method presentModalViewController.