oky_sabeni
oky_sabeni

Reputation: 7822

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"];

Upvotes: 8

Views: 9075

Answers (4)

MLBDG
MLBDG

Reputation: 1367

Try this:

[self.view.window.rootViewController dismissViewControllerAnimated:YES completion:nil];

Upvotes: 1

Rog
Rog

Reputation: 18670

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

Upvotes: 7

Soumya Ranjan
Soumya Ranjan

Reputation: 4843

Try this answer. First you create a navigation controller. make it "is initial View Controller". After that connect login Viewcontroller as a root view controller And connect home controller with facebook button Action.

Navigation Controller -> Login Controller -> Home Controller

Your Storyboard is look like this

enter image description here

After that when you logout from HomeViewController then Just add this method:

  -(IBAction)logOut_Action:(id)sender
  {
    [self.navigationController popViewControllerAnimated:YES];
  }

Its working Fine. Please implement like this and let me know if you face any problem. :)

Upvotes: 1

Vidhyanand
Vidhyanand

Reputation: 5369

U can pop by using navigationController.viewControllers.Get all View Controllers among navigationController,identify it and then pop.If u have pushed the segue from LoginView to HomeView

if([self.navigationController.viewControllers[0] isKindOfClass:[LoginViewController class]]) 
    {
      [self.navigationController popToViewController:self.navigationController.viewControllers[0] animated:YES];
    }

Hope it helps you...

Upvotes: 2

Related Questions