Sha Sha - 3 months ago 22
Objective-C Question

Remove intermediate View Controllers from Navigation Stack - iOS

I have a navigation stack as following:

VcA -> VcB -> VcC

When I press the back button on VcC navigationBar, I want to go to VcA.

I tried doing

[self.navigationController popToRootViewControllerAnimated:YES]
but it first pops to VcB and then goes to VcA.

I also tried this:

NSMutableArray *navigationArray = [[NSMutableArray alloc] initWithArray: self.navigationController.viewControllers];
[navigationArray removeObjectAtIndex: 1];
self.navigationController.viewControllers = navigationArray;


But it doesn't work. It still shows VcB first then shows VcA.

I tried both of the above code in
viewWillDisappear
of VcC.

Please help how can I solve this problem. Thanks

Answer

viewWillDisappear is too late to manipulate the view controller stack; the transition to view controller B is already underway. You should modify the stack using your second block of code in viewDidAppear so that when the back button is tapped the navigation controller transitions back to view controller A

Comments