Present storyboard view controller from app delegate?

ios, uistoryboard

Solution

You can always reference to your app delegate through the UIApplication singleton. From there you can always get your root view controller. With your root view controller you can get a reference to the storyboard.

Once you have your story board all you do is instantiate an instance of the view controller you want. Present it.

AppDelegate* appDelegate = (AppDelegate*)[[UIApplication sharedApplication] delegate];
MainViewController *mvc = (MainViewController *)appDelegate.window.rootViewController;    
LoginViewController *lvc = [mvc.storyboard instantiateViewControllerWithIdentifier:@"LoginViewController"];
[currentVC presentModalViewController:lvc animated:YES];

There may be a more direct way of getting a reference to your storyboard but this will almost always get it for you.

Problem

I have a view controller subclass, `SignInViewController`, used for sign in that might be needed at any time. Rather than have every view controller in my app listen for the notification that sign in is needed, I'd rather have the app delegate do it. But how do I trigger it from my app delegate? Do I put the `SignInViewController` in my main storyboard? If so, how do I access my storyboard from my app delegate? Or is some other approach better?

Original source