remove all uiviewcontroller from hierarchy to display the first uiviewcontroller

548 views Asked by At

When my App is starting I have a modal view controller to enter credentials (IP@, username, password...). When the user is logged he can open many UIViewControllers that can open others UIViewController and so on... These view controller can be navigation controller, tab bars, modals....

Next, the user can leave the application in background. Next, user can open the Mail App and open a mail that contains an attachment to navigate to my App. When the user select my App to open the attachment.

When the App moves in foreground I need to go to a specific viewController of the hierarchy (the first ViewController opened after the login screen).

To move the App to this first ViewController I have tried to use – dismissViewControllerAnimated:completion:

but without success, there're still a view from the hierarchy that is still displayed.

Any idea how to do this? Regards,

Sebastien.

2

There are 2 answers

0
sebastien On

I found a solution with this small piece of code

    UINavigationController* navc = (UINavigationController*)viewController.topViewController;
if (navc != Nil) {
    if (navc.presentedViewController != Nil) {
        [navc dismissViewControllerAnimated:FALSE completion:Nil];
    }
        [navc popToRootViewControllerAnimated:TRUE];
}

It was not complex at the end!

Sébastien.

0
Darren On

If all the view controllers are in 1 Navigation controller and pushed onto the stack, you can simply use either popToRootViewControllerAnimated: or popToViewController:animated:

Otherwise, why not dismiss the first Modal view controller, then put it back with the view controller you want.