2
votes

I've got a series of nested navigation controllers (because I want the nav bar at the top and I can't get that bar to look correct without using the nav controller).

So basically it looks like this: -NavigationController-VC1-->(Modal)--NavigationController-VC2-->(modal)...etc

When I'm at the summary ViewController, the final one - I want a button to take the user back to the very root ViewController. How can I do this?

I've tried the popToRootViewControllerAnimated but that doesn't work as I've got a series of navigation controllers (better solution??)

Thanks!

Edit

Navigation now:

Navigation Controller --> VC1 --(modal)-->VC2 --(modal)-->VC3... and so on

4

4 Answers

0
votes

popToRootViewController:Animated will only go back to [0] of your navigation controller's array of view controllers. So in other words, the root of the first navigation controller will always be VC1, and the root of your second navigation controller will always go to VC2. You should reconsider the structure of your app, because you shouldn't have one navigation controller push to another - it doesn't make sense. Here's what you can do:

Navigation Controller -> VC1 -> (push segue) -> VC2 -> (push segue) -> VC3

This way popToRootViewController:Animated will go back to VC1.

The only reason I would use more than one navigation controller is if I had some sort of a table with options such as:

---
A     Navigation Controller -> VCA1 -> (push segue) -> VCA2 -> (push segue) -> VCA3
---
B     Navigation Controller -> VCB1 -> (push segue) -> VCB2 -> (push segue) -> VCB3
---
C     Navigation Controller -> VCC1 -> (push segue) -> VCC2 -> (push segue) -> VCC3
---

Even in this case, the root view controller will always be VCA1, VCB1, or VCC1.

Hope this helps.

It would also help if you post what you are trying to accomplish so we can better understand your question.

0
votes

By Writing the First Line you get the Indexes of all View Controllers and from second Line You will reach up to your Destination.

NSArray *array = [self.navigationController viewControllers];

[self.navigationController popToViewController:[array objectAtIndex:2] animated:YES];

0
votes

You don't say whether your using storyboards or not but if you are try looking into unwind segues. What are Unwind segues for and how do you use them?

Using an unwind segue should get you back to where you need to be in the hierarchy.

0
votes

If you are use same navigation controller to all view controller than this line works fine for you.

[self.navigationController popToRootViewControllerAnimated:YES];

but in your case you are presenting model view controller.for this case you need to dismiss first model view controller before presenting other one.