0

Using storyboard i have created some flow of 4 view controllers . I have a UINavigation cotroller, that has some view controller as its root view . than from that root view i can go all the way to the 4th view .

Each was made with story board, and has its button , that i have control dragged to the next page, and set it as a modal transition` from the small menu.

Now when i dismiss the last view with :

[self dismissViewControllerAnimated:YES completion:nil];

I get to the previous view, which is good.

I would like now to remove all this stack including the navigation , with :

popToRootViewControllerAnimated:

Which is doing nothing.

Binarian
  • 12,296
  • 8
  • 53
  • 84
Curnelious
  • 1
  • 16
  • 76
  • 150
  • So each ViewController is a modal on top of the previous one? or are you using the push method for some of them? – Tiago Lira May 25 '14 at 10:11
  • the popToRootViewController is used for viewcontroller stacks using the "push" method. dasblinkenlight's answer is right, that is how you remove the modals. If you want to control it from the navigationController with popToRootViewController you should use "push" instead of "modal", which may be a better approach for your situation. – Tiago Lira May 25 '14 at 10:59
  • the reason i didn't wanted to push is because the upper bar that you get when you push ,and that i don't want .. – Curnelious May 25 '14 at 11:17
  • ok , i manage to hide it and push views, now i am trying again to remove all of them ,but pop to root is going to the first one and not removing him also as i want.. – Curnelious May 25 '14 at 11:25
  • ok i mange to remove all of them with : [[self.navigationController.viewControllers objectAtIndex:0] dismissViewControllerAnimated:YES completion:nil]; is this good ? – Curnelious May 25 '14 at 11:27

1 Answers1

2

If you want to go all the way back to the root view controller, use this code:

[self.view.window.rootViewController dismissViewControllerAnimated:YES completion:nil];

Note: if you wish to dismiss all the additional view controllers from the context of an application delegate, say, in response to the application going into background, pass NO for the value of the animated parameter.

The reason I didn't wanted to push is because the upper bar that you get when you push ,and I don't want that

Navigation view controller lets you hide the navigation bar if you do not want end-users to see it.

Community
  • 1
  • 1
Sergey Kalinichenko
  • 714,442
  • 84
  • 1,110
  • 1,523
  • but why popToRootViewControllerAnimated: is not working for me ? i have a navigation view controller, that has some initial view as a root view, than this view is take you to another view via modal , than from that end view i am trying to remove all the stack of the navigation ,is that what your code will do ? – Curnelious May 25 '14 at 10:51
  • @Curnelious `popToRootViewControllerAnimated` works only when your segues push view controllers onto a stack of a navigation view controller, not when segues present view controllers modally. With modally presented view controllers you have no stack of view controllers, so there is nothing to "pop". – Sergey Kalinichenko May 25 '14 at 11:04
  • Ok , so how would one go back and remove all the last 3 views that came after the navigation controller (and btw what benefit navigation controller have if i just present modally ? ) – Curnelious May 25 '14 at 11:15
  • @Curnelious If you present all your view controllers modally, you do not need a navigation controller. – Sergey Kalinichenko May 25 '14 at 11:20
  • Ok ,i now got it , so i am pushing views now and it works fine, but now i want to remove all stack from the superview , so if i use the popToRoot it takes me to the first one , but i want to remove all of them,including the first ,how would i do that ? – Curnelious May 25 '14 at 11:21
  • [[self.navigationController.viewControllers objectAtIndex:0] dismissViewControllerAnimated:YES completion:nil]; is working, is that good ? – Curnelious May 25 '14 at 11:27
  • @Curnelious Yes, you can use `self.navigationController.viewControllers` array to pick any controller on the stack, including the top one at index zero. – Sergey Kalinichenko May 25 '14 at 11:37