2

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

Erik
  • 2,500
  • 6
  • 28
  • 49

4 Answers4

0

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.

Carpetfizz
  • 8,707
  • 22
  • 85
  • 146
  • The reason I did this was because the top bar where I had the navigation button was too big If I added an empty prompt, but too small If I didn't. The NavigationController made it perfectly big. I'll try to restructure my app to make it more organized ;) The concept here is sort of like a wizard, adding a new item and adding things to that item along the way. It'll be used for work stuff – Erik Sep 07 '14 at 21:18
  • @Erik, oh okay cool. Remember that you can subclass elements to make them look like the way you want them to if you don't want to deal with the extra complexity of the navigation controller. Also you mentioned that its a wizard and that you'll be "adding" prompts, etc. In my opinion all the scenarios should be loaded already and shouldn't be dynamically generated along the way. – Carpetfizz Sep 07 '14 at 21:21
  • 1
    Yeah. Thanks!;) - I'll get some sleep now and continue tomorrow, getting late here – Erik Sep 07 '14 at 21:38
  • I tried popToRootViewController:Animated now, but nothing is happening. I think the code is being called as an NSLog after it does fire, but it doesn't dismiss anything. Any ideas? @Carpetfizz – Erik Sep 08 '14 at 13:40
  • @Erik, if something happens and there is no error that means you are most probably AT the root view controller. – Carpetfizz Sep 08 '14 at 18:43
  • I don't understand that, I'm at like number 6 in a series of ViewControllers being modally displayed one after another. Shall I post a screenshot of the ViewController layout?? @Carpetfizz – Erik Sep 09 '14 at 15:53
  • 1
    I solved the problem using pidge's answer, though I would like to solve the problem with the popToRootViewController:animated. Here's a screenshot of the ViewControllers: http://imgur.com/y2A1GaM @Carpetfizz – Erik Sep 09 '14 at 16:27
0

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];

  • I tried this now but absolutely nothing is happening :/ - did a NSLog after popToViewController and it does log, but it doesn't pop to the viewcontroller. Any ideas why? See my updated question for how the navigation looks now – Erik Sep 08 '14 at 13:37
0

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.

Community
  • 1
  • 1
pidge
  • 20
  • 3
0

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.

dheeru
  • 395
  • 3
  • 13
  • I could use that approach, but then the user wouldn't be able to go backwards in the "wizard" – Erik Sep 08 '14 at 10:28