8

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"];
oky_sabeni
  • 7,672
  • 15
  • 65
  • 89

4 Answers4

7

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

Rog
  • 18,602
  • 6
  • 76
  • 97
  • 1
    I got it to work but I'm working how to do if you want to match the segue action to a UIActionSheet button (i.e. there is no logout button or the button is generated programmatically) – oky_sabeni Feb 13 '15 at 20:53
  • 1
    Connect the controller (first icon of the three where the Exit segue icon is) to the Exit icon. Give the unwind segue a name and when you want to trigger it just use the `performSegueWithIdentifier:sender:` method. – Rog Feb 14 '15 at 09:59
2

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...

Vidhyanand
  • 5,369
  • 4
  • 26
  • 59
1

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. :)

Soumya Ranjan
  • 4,817
  • 2
  • 26
  • 51
1

Try this:

[self.view.window.rootViewController dismissViewControllerAnimated:YES completion:nil];
MLBDG
  • 1,357
  • 17
  • 23