Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I return to the first view controller on a storyboard?

Here is the scenario:

The first scene in my storyboard is a login view. It's a UIViewController. When the user is logged in, it shows the home view which is embedded in a navigation controller. I'm adding a log out functionality which should take me back to the first scene in the storyboard which is the login view. How do I do that?

Here is an image of the storyboard showing the login view -> navigation controller -> home view Storyboard

This is my implementation so far. In the log out action, I clear the session, and pop to root view controller. It does not work because I am still stuck on the home view since it is the root view controller of the navigation controller. However, If I restart the app, the user is logged out and I'm left with the login view.

Code:

    [self.navigationController popToRootViewControllerAnimated:NO];

    // Set at beginning of storyboard
    UIStoryboard *mystoryboard = [UIStoryboard storyboardWithName:@"Main" bundle:nil];
    AppDelegate *app = (AppDelegate *)[[UIApplication sharedApplication] delegate];
    app.loginViewController = [mystoryboard instantiateViewControllerWithIdentifier:@"loginViewController"];
like image 353
okysabeni Avatar asked Feb 13 '15 03:02

okysabeni


1 Answers

Use unwind segues for that.

In your LoginViewController, declare a method with this signature

- (IBAction)unwindToLoginViewController:(UIStoryboardSegue*)segue

Go to your HomeViewController and control drag from your logout button to the Exit button at the top of your view controller window (see screenshot below), then select the unwindToLoginViewController segue. That's it!

enter image description here

like image 172
Rog Avatar answered Sep 28 '22 04:09

Rog