Display modal view when running ipad application

I want to conditionally display the login screen when starting the ipad application. I do not want this to be part of the standard segue, as they should only be logged periodically, and not every time.

There are numerous examples of my question, but they all seem to precede ios5. However, when I use storyboards, nothing works.

To reduce this in essence, * create a new one-time application using the storyboard * add a new viewcontroller to the storyboard, give it the loginScreen identifier * place a text label on each view to visually distinguish them. * in 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 saw in the examples, this should work. But it still constantly displays the original rootViewController view. There are no errors.

Can someone point out a (possibly small) thing that I will skip?

+6
source share
2 answers

It turns out that the application is not in the active state in the didFinishLaunching method.

The right place to place is

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

@deafgreatdane: your solution will present a view controller modulo each time the application becomes active from the background state (which may be desirable).

In my case (using this to show a one-time launch screen) I would add dispatch_once to this solution to make sure that the modal launch screen appears only 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 piece of code uses ARC.

+2
source

Source: https://habr.com/ru/post/910265/


All Articles