如何从导航控制器中删除视图
我想调用一个新的视图控制器并从导航控制器堆栈中删除当前的视图控制器。 例如。我在视图控制器 A 中,我调用 B。
现在我在堆栈中 A , B 。 现在我想给C(B)打电话。 我希望堆栈是 A、C。
谢谢。
I want to call a new view controller and remove the current view controller from the navigation controller stack.
For example. I am in view controller A and I call B.
Now I have in the stack A , B.
Now I want to call C (from B).
I want the stack to be A, C.
Thanks.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
在 ARC 的上下文中,这是一个可能的解决方案:
如您所知,此代码替换了您尝试从堆栈中删除的视图中的常用推送代码(问题中的“B”)。第 1 行将视图控制器列表从导航控制器堆栈复制到 NSMutableArray 中。第 2 行将最后一个(最顶层)视图替换为我们要转到下一个视图(“C”)。第 3 行使实际导航控制器的堆栈成为我们更改后的数组,并以动画方式过渡到最顶层的项目。 (当然,您不能在根 viewController 中使用此代码。)
我在此处找到了一种替代方法,并将其改编为ARC:
需要第一行,因为一旦您将当前视图从堆栈中弹出,
self.navigationController
将是nil
并且第三行将不起作用。行数与前一种方式相同,但这种方式通过内置方法而不是“手动”摆弄堆栈来工作。In the context of ARC, here's a possible solution:
As you can tell, this code replaces the usual push code in the view you're trying to remove from stack ("B," in your question). Line 1 copies the list of view controllers from the nav-controller stack into an
NSMutableArray
. Line 2 replaces the last (topmost) view with the view we want to go to next ("C"). Line 3 makes the actual nav-controller's stack to be our altered array, and animates the transition to the topmost item. (Of course you can't use this code in the root viewController.)I found an alternative way here and adapted it for ARC:
The first line is needed because once you've popped the current view off the stack,
self.navigationController
will benil
and the third line won't work. Same number of lines as the previous way, but this way works through built-in methods instead of "manually" fiddling with the stack.这就是答案。
以下代码弹出当前视图控制器。
这推动了新的:
希望它有帮助!
This is the answer.
The following code pops the current view controller.
And this pushes the new one:
Hope it helps!
要删除倒数第二个导航项:
例如,从控制器 C 上的
viewDidLoad
运行此命令,以从导航堆栈中删除控制器 B。To remove the second-from-last navigation item:
For example, run this from
viewDidLoad
on controller C to remove controller B from the navigation stack.我认为你可以通过首先从导航堆栈中弹出 B 然后将 C 推入其中来做到这一点。您应该能够使用 [UINavigationController popViewControllerAnimated] 和 [UINavigationController PushViewController:animated] 来实现此目的。
I think you can do this by first popping B from navigation stack and then pushing C into it. You should be able to use [UINavigationController popViewControllerAnimated] and [UINavigationController pushViewController:animated] for this.
您可以使用此代码。
NSMutableArray *navigationArray = [[NSMutableArray alloc] initWithArray: self.navigationController.viewControllers];
[navigationArray 删除所有对象]; self.navigationController.viewControllers = navigationArray;
希望这对你有用。
You can use this code.
NSMutableArray *navigationArray = [[NSMutableArray alloc] initWithArray: self.navigationController.viewControllers];
[navigationArray removeAllObjects]; self.navigationController.viewControllers = navigationArray;
Hope that will work for you.