Search code examples
iosback-stack

Clearing back stack on transition


I have a storyboard set up using modal transitions as follows:

root -> A -> B -> C

When I transition from B to C, I want to clear the backstack because a user will never be able to get back to A, B or root. I know it is not possible to remove the root view controller but is there a way to remove A and B from the stack when transition to C.

I have Android background and in there it can be done by simply adding CLEAR_TOP and NEW_TASK flag to the intent before starting next activity. Is there something similar in iOS?

I am trying to call this on the on the transition from B->C but it crashes the app.

[self dismissViewControllerAnimated:NO completion:nil];

Also tried this on view did Load of B but it doesn't work.

[self.presentingViewController dismissViewControllerAnimated:NO completion:nil];

EDIT: Is it possible to make C the root and clear A->B and previous root. How would you do that?


Solution

  • All you have to do is set yourself as the window's root view controller in the viewDidAppear method of C.

    - (void)viewDidAppear:(BOOL)animated {
        [super viewDidAppear:animated];
        self.view.window.rootViewController = self;
    }
    

    Root, A, and B will be deallocated when you do this.