How do I move the view onto the navigation stack and remove the current one?

I would like to push the view on the navigation controller stack, but delete the current one, so when users go back to the next view, it won't bring them back to the previous view.

The flow in one direction can be one of the following:

View X -> View 1 -> View 2 -> View 3    
View X -> View 1      ->      View 3

      

But the flow in the opposite direction is always like this:

View X <- View 1      <-      View 3

      

The problem is that I want to handle it on the View 2 controller, doing it on View 3, easily overriding the back button action. There can be many different views in View 3, and I don't want to override the back button for all of them and check if the previous stack controller is the View 2 controller. They all have the same parent class, so I can't override the button. " Back "only for controllers" Class 3 view.

I tried this so the View 2 controller is not added to the navigation stack:

//Pop controller from stack before pushing
[self.navigationController popViewControllerAnimated:YES];
[self.navigationController pushViewController:controller animated:YES];

      

But after you hit it removes the view and doesn't click on the next controller

Another option with the same result

NSMutableArray *viewControllers = [NSMutableArray arrayWithArray: self.navigationController.viewControllers];
[viewControllers removeObjectIdenticalTo:self];
self.navigationController.viewControllers = viewControllers;
[self.navigationController pushViewController:controller animated:YES];

      

+3


source to share


1 answer


Regarding your last example, which doesn't push, does this work?

NSMutableArray *viewControllers = [NSMutableArray arrayWithArray: self.navigationController.viewControllers];
[viewControllers removeObjectIdenticalTo:self];
[viewControllers addObject:controller];
[self.navigationController setViewControllers: viewControllers animated: YES];

      



Since you are setting controllers to an array of controllers without you, you can set your property to a navigationController

value nil

so you won't be able to push a new one right away. It doesn't hurt anyway.

+11


source







All Articles