Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 586
deafgreatdane Avatar asked Mar 08 '12 17:03

deafgreatdane


People also ask

What is modality of an app?

Modality is a design technique that presents content in a separate, focused mode that prevents interaction with the parent view and requires an explicit action to dismiss. Presenting content modally can: Ensure that people receive critical information and, if necessary, act on it.

How do I present a view controller in Objective C?

Set the modalPresentationStyle property of the new view controller to the desired presentation style. Set the modalTransitionStyle property of the view controller to the desired animation style. Call the presentViewController:animated:completion: method of the current view controller.

How do you make a modal segue?

Modal segues can be created by simply ctrl-click and dragging to destination but when I do that with the push my app crashes.


2 Answers

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];
}
like image 192
deafgreatdane Avatar answered Nov 15 '22 11:11

deafgreatdane


@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.

like image 29
Michael Thiel Avatar answered Nov 15 '22 12:11

Michael Thiel