Note that there are some explanatory texts on larger screens.

plurals
  1. POselecting alternative first view controller from story board at application startup
    text
    copied!<p>I've just started on iOS programming and so far the tutorials and answers I found here have been a great help to move forward. However, this particular problem has been bumming me all night and I can't find an answer that "feels right".</p> <p>I'm writing an application that connects to a remote service and the users need to sign in before they can use it. When they start using the application, their first view should be the sign in dialog; when they've authenticated before, they immediately see the overview page.</p> <p>The project uses story boards - which I think is a great feature - so most of the code that selects and loads the root view controller is already taken care of. I thought the best place to add my logic is the <code>application:didFinishLaunchingWithOptions:</code> method of the <code>AppDelegate</code>:</p> <pre><code>- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions: (NSDictionary *)launchOptions { // select my root view controller here based on credentials present or not return YES; } </code></pre> <p>But this brought up two questions:</p> <ol> <li><p>Inside this particular delegate method, the root view controller has already been selected (and loaded?) based on the story board. Could I move to an earlier spot in the loading process to override the first view controller selection or would that needlessly complicate matters?</p></li> <li><p>To override the first view controller I need a reference to the story board, but I couldn't find a better way than to use the <code>storyboardWithName:bundle:</code> constructor of <code>UIStoryboard</code>. That feels wrong, the application should already have a reference to the story board, but how can I access it?</p></li> </ol> <p><strong>Update</strong></p> <p>I worked out the second issue I was having, as I found my answer here:</p> <p><a href="https://stackoverflow.com/questions/9853608/uistoryboard-whats-the-correctest-way-to-get-the-active-storyboard">UIStoryboard: What&#39;s the Correct Way to Get the Active Storyboard?</a></p> <pre><code>NSBundle *bundle = [NSBundle mainBundle]; NSString *sbFile = [bundle objectForInfoDictionaryKey:@"UIMainStoryboardFile"]; UIStoryboard *sb = [UIStoryboard storyboardWithName:sbFile bundle:bundle]; </code></pre> <p>The above will create a new story board instance; to get the active instance, it's a whole lot simpler:</p> <pre><code>UIStoryboard *sb = [[self.window rootViewController] storyboard]; </code></pre> <p>In the story board file itself you have to set an identifier for the view you wish to load, e.g. <code>LoginDialog</code>. Afterwards you instantiate the view like this:</p> <pre><code>LoginViewController *login = [sb instantiateViewControllerWithIdentifier:@"LoginDialog"]; [self.window setRootViewController:login]; </code></pre> <p>Within another view controller, the following suffices:</p> <pre><code>UIStoryboard *sb = self.storyboard; LoginViewController *login = [sb instantiateViewControllerWithIdentifier:@"LoginDialog"]; [self presentViewController:login animated:NO completion:nil]; </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload