如何从导航堆栈中的任何位置弹出到任意视图控制器
我正在开发一个基于导航的应用程序,我选择它是为了我可以在堆栈上推送和弹出控制器。继续前进很容易 - 只需推一个新控制器即可。只要您转到根视图控制器,向后移动就很容易。有一个名为 popToViewController:animated,
的方法,但我没有创建要从当前视图控制器中弹出的控制器,因此编译器抱怨我尚未声明它。我知道它是堆栈上的第二个控制器(根上方的一个)。我可以用它到达那里吗?
I'm working on a navigation based app, and I chose it so that I can push and pop controllers on and off the stack. It's easy enough to move forward - just push a new controller. And moving back is easy as long as you go to the root view controller. There is a method called popToViewController:animated,
but I didn't create the controller I want to pop to from within my current view controller, so the compiler complains that I haven't declared it. I know it's the second controller on the stack (one above the root). Can I use that to get there?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
UINavigationController 的 viewControllers 属性具有视图控制器,以便它们被推送,因此您可以使用它以及您对要弹出到该视图控制器的视图控制器的了解。这里是一个参考 UINavigationController 参考
The viewControllers property of a UINavigationController has the viewControllers in order that they were pushed, so you can use that and your knowledge of which view controller it is to pop to that view controller..here is a reference UINavigationController ref
我通常创建一个 NavigationController 对象,它了解我的 UINavigationController 和 viewControllers。如果你为每个 VC 提供对这样的对象的引用,或者将其设置为单例,那么它可以为你处理这样的事情。
在视图控制器中嵌入导航逻辑并没有什么问题,但是当它们了解所有其他视图控制器时,可能会使它们更难维护。将导航逻辑封装在共享对象中使您的应用程序更易于理解和维护。
青年MMV
I generally create a NavigationController object which has knowledge about both my UINavigationController and my viewControllers. If you give each of your VCs a reference to an object like this, or make it a singleton, then it can handle things like this for you.
There is nothing wrong with embedding navigation logic in view controllers, but it can make them harder to maintain when they know about every other view controller. Encapsulating navigation logic in a shared object makes your app easier to understand and maintain.
YMMV