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?
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.
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.
Modal segues can be created by simply ctrl-click and dragging to destination but when I do that with the push my app crashes.
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];
}
@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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With