Pop SWRevealViewController

1.2k Views Asked by At

I'm using SWRevealViewController to build a sliding menu. Before accessing the menu, a login view is displayed to allow the user login. Now i want to let the user disconnect and display the login view again. My question is how to pop the SWRevealViewController.

code that provides access to application after login is:

MainViewController *vMainMenu = [[MainViewController alloc] initWithNibName:@"MainViewController" bundle:nil];
    vMainMenu.strURL = URL;
    LeftMenu *vLeftMenu = [[LeftMenu alloc] initWithNibName:@"LeftMenu" bundle:nil];

    UINavigationController *principalNavController = [[UINavigationController alloc] initWithRootViewController:vMainMenu];
    UINavigationController *rearNavController = [[UINavigationController alloc] initWithRootViewController:vLeftMenu];

    SWRevealViewController *mainRevealController = [[SWRevealViewController alloc] initWithRearViewController:rearNavController frontViewController:principalNavController];

    mainRevealController.delegate = self;

    self.viewController = mainRevealController;

    principalNavController.navigationBar.hidden = YES;
    rearNavController.navigationBar.hidden = YES;

    self.window.rootViewController = self.viewController;

This solution is not working:

[self.revealViewController.navigationController popViewControllerAnimated:YES];
Splash *vSplash = [[Splash alloc] initWithNibName:@"Splash" bundle:nil];
[self.navigationController pushViewController:vSplash animated:YES];

Can you help me please

Thank you.

3

There are 3 best solutions below

0
On

You can use an "Unwinding Segue". The unwinding segue is like a regular segue which goes back instead of forward.

First, go to the Login View Controller and add the following method:

-(IBAction)unwindToLoginScreen:(UIStoryboardSegue *)segue {
}

The method may be empty and it can have any name; it is good to use descriptive names because you might have many unwind segues on your code and it will be useful to be able to distinguish between them.

This unwind method will act as a global constant that will be visible to all View Controllers on the storyboard.

Now, on each view controller that should be able to return to the Login Screen you create a Unwind Segue. You can do this on the Storyboard:

  1. Select the View Controller you want; on the top bar you should see three buttons, one representing the View Controller, another representing the First Responder and another representing an Exit.
  2. Ctrl+Drag from the View Controller button to the Exit button. A context menu will appear and will list all visible unwind methods you created on your storyboard.
  3. On the Document Outline, an "Unwind segue" will appear for the view controller. You can add an identifier to that segue (such as "Login" in your case). Then you call performSegueWithIdentifier. Select the one you created on your Login view controller.

Doing this, the complete stack between the view controller and the login view controller will be unwinded. That is, all view controllers stacked above the Login view controller will be popped.

You can also use prepareForSegue for the unwind segues the same way you would use them for regular segues. And you can add code to the -(IBAction) method if you want to do something as a result to the segue.

0
On

You can't go back to your LoginViewController by pop. You can add PresentViewController without animation to do this.

Splash *vSplash = [[Splash alloc] initWithNibName:@"Splash" bundle:nil];
[self presentViewController: vSplash animated:NO completion:nil];
0
On

I had the same issue. I have implemented several storyboards.

        let storyboard = UIStoryboard(name: "Login", bundle: nil)
        let startcontroller = storyboard.instantiateViewControllerWithIdentifier("LoginVC") as UIViewController
        window?.rootViewController = startcontroller

With this snippet you can access the right View Controller.