deafgreatdane
deafgreatdane

Reputation: 1201

display a modal view on ipad application startup

I want to conditionally display a login screen on launch of an ipad app. I don't want to make it part of a default segue, since they only need to login periodically, not every time.

There are numerous examples of my question, but they all seem to predate ios5. When I use storyboards, however, nothing seems to work.

To reduce this to its essence, * create a new single view application, using storyboard * add a new viewcontroller to the storyboard, give it an identifier of "loginScreen" * put a text label on each view to visually distinguish them. * in the appDelegate:

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
    UIStoryboard *storyboard = [self.window.rootViewController storyboard];
    UIViewController *loginController = [storyboard instantiateViewControllerWithIdentifier:@"loginScreen"];
    [self.window.rootViewController presentModalViewController:loginController animated:TRUE];

    return YES;
}

From what I've seen of the examples, that should work. But it still consistently displays the original rootViewController's view. No errors though.

Can anyone point out the (probably small) thing I'm missing?

Upvotes: 6

Views: 1719

Answers (2)

Michael Thiel
Michael Thiel

Reputation: 2444

@deafgreatdane: Your solution would present the view controller modally each time the application becomes active from being in a background state (which may be desirable).

In my case (using this to show a one-time only launch screen) I would add a dispatch_once to that solution to make sure that the modal launch screen will only show up once:

- (void)applicationDidBecomeActive:(UIApplication*)application
{
   static dispatch_once_t onceToken;

   dispatch_once( &onceToken, ^
                 {
                    SomeLaunchViewController* launchViewController = [[SomeLaunchViewController alloc] init];
                    [self.window.rootViewController presentViewController:launchViewController animated:NO completion:NULL];
                 } );
}

This code snippet uses ARC.

Upvotes: 2

deafgreatdane
deafgreatdane

Reputation: 1201

It turns out that the app isn't in an active state in the didFinishLaunching method.

The proper place to put this is

- (void)applicationDidBecomeActive:(UIApplication *)application
{
    UIStoryboard *storyboard = self.window.rootViewController.storyboard;
    UIViewController *loginController = [storyboard instantiateViewControllerWithIdentifier:@"loginScreen"];
    [self.window.rootViewController presentModalViewController:loginController animated:NO];
}

Upvotes: 2

Related Questions