简体   繁体   English

弹出viewcontroller然后回到RootViewController似乎不起作用

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

I have an app that pushes four viewcontrollers on the stack. 我有一个应用程序,在堆栈上推送四个viewcontrollers。 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. 在第四个视图控制器中,我做了一个PopViewController,所以我应该回到第三个viewcontroller,在viewWillAppear方法中,我做一个PopToRootViewController。 This does not get me back to the first viewcontroller correctly. 这不会让我回到正确的第一个viewcontroller。 The code is straightforward, just a 代码很简单,只是一个

[self.navigationController pushViewController:nextController animated:YES]

in each of the first 3 viewcontrollers, and in the fourth 在前3个视图控制器中的每一个中,以及在第四个中

[self.navigationController popViewControllerAnimated:YES].  

In the 3rd viewcontroller, I have a viewWillAppear method which does: 在第三个viewcontroller中,我有一个viewWillAppear方法,它执行:

[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: 按OK:弹出1然后弹回root:

         Back: FirstLevel    Title: ThirdLevel

If I press OK now: 如果我现在按OK:

         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. 正如Mark建议的那样,你应该使用你的-popToRootViewController视图控制器中的-popToRootViewController而不是第3个视图控制器,因为你的动机是将它弹出到根视图控制器。

BUT if you still want to achieve this functionalty for some anonymous reason, then in the 3rd viewcontroller, use : 但是如果您仍然想要出于某些匿名原因而实现此功能,那么在第3个viewcontroller中,使用:

[self.navigationController popToRootViewControllerAnimated:YES] in viewDidAppear method, instead of viewWillAppear . viewDidAppear方法中的[self.navigationController popToRootViewControllerAnimated:YES] ,而不是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. 似乎没有任何理由回弹查看控制器3,因为您甚至在视图控制器3的视图出现之前尝试转到根。 Just send -popToRootViewControllerAnimated: to view controller 4. 只需发送-popToRootViewControllerAnimated:即可查看控制器4。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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