Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Present storyboard view controller from app delegate?

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?

like image 646
Steven Fisher Avatar asked Apr 19 '12 18:04

Steven Fisher


People also ask

How do I get a view controller from storyboard?

In the Storyboard, select the view controller that you want to instantiate in code. Make sure the yellow circle is highlighted, and click on the Identity Inspector. Set the custom class as well as the field called "Storyboard ID". You can use the class name as the Storyboard ID.

What is the difference between app delegate and scene delegate?

The app delegate, by default, will use the default scene configuration. The scene delegate, by default, sets up a UIWindow object, and uses the Main. storyboard to create the initial UI.


1 Answers

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.

like image 93
endy Avatar answered Oct 19 '22 13:10

endy