Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Going back to initial storyboard ViewController on logout

I am using storyboards in my iOS. The first screen is the login screen. When a user logs out or gets logged out, he may be on a screen in a deep hierarchy.

For example: login view controller => modal view controller => tab bar controller => nav controller => view controller => view controller. I want to go all the way back from the top-most view controller to the bottom one.

Edit: Here's a diagram of the view hierarchy:enter image description here

Thanks!

like image 578
Venkat D. Avatar asked Oct 22 '12 16:10

Venkat D.


2 Answers

I wrote a category for UIViewControllers that seems to be working:

- (void) popToInitialViewController
{
    UIViewController *vc;
    if (self.navigationController != nil) {
        vc = self.navigationController;
        [self.navigationController popToRootViewControllerAnimated:NO];
        [vc popToInitialViewController];
    }
    else if (self.tabBarController != nil) {
        vc = self.tabBarController;
        [vc popToInitialViewController];
    }
    else if (self.presentingViewController != nil) {
        vc = self;

        while (vc.presentingViewController != nil)
            vc = vc.presentingViewController;

        [vc dismissModalViewControllerAnimated:NO];

        [vc popToInitialViewController];
    }
}

Comments are appreciated :)

like image 58
Venkat D. Avatar answered Oct 16 '22 15:10

Venkat D.


this should work assuming everything was pushed onto the navigation stack:

[self.navigationController popToRootViewControllerAnimated:YES];
like image 2
Edwin Iskandar Avatar answered Oct 16 '22 16:10

Edwin Iskandar