简体   繁体   中英

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.

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 :)

There doesn't appear to be any reason to pop back to view controller 3, since you're trying to go to the root before view controller 3's view even appears. Just send -popToRootViewControllerAnimated: to view controller 4.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM