Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Popping viewcontroller and then back to RootViewController doesn't seem to work

I have an app that pushes four viewcontrollers on the stack. In the fourth view controller, I do a PopViewController, so I should be back at the 3rd viewcontroller, and in the viewWillAppear method, I do a PopToRootViewController. This does not get me back to the first viewcontroller correctly. The code is straightforward, just a

[self.navigationController pushViewController:nextController animated:YES]

in each of the first 3 viewcontrollers, and in the fourth

[self.navigationController popViewControllerAnimated:YES].  

In the 3rd viewcontroller, I have a viewWillAppear method which does:

[self.navigationController popToRootViewControllerAnimated:YES].

As I go through the views, I get the following:

Start app:

         Back:               Title: FirstLevel

Press OK:

         Back: FirstLevel    Title: SecondLevel

Press OK:

         Back: SecondLevel   Title: ThirdLevel

Press OK:

         Back: ThirdLevel    Title: FourthLevel

Press OK: which pops back 1 and then pops back to root:

         Back: FirstLevel    Title: ThirdLevel

If I press OK now:

         Back: ThirdLevel    Title: SecondLevel

I'm sorry if this is confusing. Should I be able to pop back one view controller and then immediately pop back to the root?

Thanks for any help.

like image 832
DNewell Avatar asked Mar 02 '11 20:03

DNewell


People also ask

How do I dismiss a specific view controller in Swift?

Use unwind segue instead of using RootViewController. Using unwind you can go back to any ViewController. DismissViewController always send the controller out from NavigationController.


1 Answers

As suggested by Mark, you should use -popToRootViewController from your 4rth view controller, instead of 3rd view controller, as your motive is to pop it to root view controller.

BUT if you still want to achieve this functionalty for some anonymous reason, then in the 3rd viewcontroller, use :

[self.navigationController popToRootViewControllerAnimated:YES] in viewDidAppear method, instead of viewWillAppear.

I hope it works :)

like image 162
iHS Avatar answered Nov 15 '22 19:11

iHS