我有一个 NavigationController
,其中嵌入了以下 VC:VC1 -> VC2 -> VC3 -> VC4 -> VC5.我的问题是,当我从 VC5 切换时(编辑完成后),我将您送回 VC3,但我想以编程方式将 VC4 和 VC5 从堆栈中抛出,即当用户被送回 VC3 时,我想要返回" 在 navagitionBar
中将您带到 VC2(而不是您真正来自的 VC5).
I have a NavigationController
that the following VC's are embedded in: VC1 -> VC2 -> VC3 -> VC4 -> VC5. My problem is that when I segue from VC5 (after editing is completed), I send you back to VC3, but I want to programmatically throw VC4 and VC5 off the stack, i.e. when the user is sent back to VC3, I want "back" in the navagitionBar
to take you to VC2 (and not VC5 where you really came from).
这在 IOS 中经常出现,您想在其中编辑模型,然后将它们发送回 tableView/Collection 视图,但由于编辑完成,您不再希望在导航堆栈中编辑 viewControllers它对用户体验太混乱了.
This comes up a lot in IOS, where you want to edit the model, then send them back to the tableView/Collection view, but since editing is done, you don't want the editing viewControllers in the navigation stack anymore as its too confusing of UX.
在下面的截图中,右上角的VC是VC5: 通过 self.performSegueWithIdentifier("backToPins" 连接到 PinViewController (VC3),发件人:自己)
In the screenshot below, the VC on the top right is VC5: which is segued back to the PinViewController (VC3) via self.performSegueWithIdentifier("backToPins", sender: self)
我该怎么做?
不要使用 segue
回来(pop).
don't use segue
to come back (pop).
您应该使用 popToViewController
并传递特定的 viewcontroller
作为参数来弹出该 viewcontroller.
you should use popToViewController
and pass specific viewcontroller
as argument to pop that viewcontroller.
例如,如果您想在五个视图控制器中使用第三个视图控制器,那么您可以执行以下操作.您可以将视图控制器数组中的索引更改为不同的视图控制器.
for example if you want to go on 3rd view controller out of five then you can do something like below. you can just change index from viewcontroller array to go different view controller.
let viewControllers: [UIViewController] = self.navigationController!.viewControllers as [UIViewController];
self.navigationController!.popToViewController(viewControllers[viewControllers.count - 3], animated: true);
如果您使用 segue,则意味着您将新的视图控制器添加(推送)到导航堆栈.在您的示例中,您在达到第 5 个视图后的堆栈就像,
If you are using segue that means you add (push) new viewcontroller to navigation stack. in your example your stack after reaching 5th view is like,
VC1 - VC2 - VC3 - VC4 - VC5(栈顶)
现在如果你执行egue回到VC3那么堆栈应该是这样的,
now if you performsegue to go back to VC3 then stack should be like this,
VC1 - VC2 - VC3 - VC4 - VC5 - VC3(栈顶)
如果你弹出到 VC3,那么你的堆栈就像,
and if you pop to VC3 then your stack is like,
VC1 - VC2 - VC3(栈顶)
.
所以 pop viewcinrollers 回去不要使用 segue
so pop viewcintrollers to go back don't use segue
希望这会有所帮助:)
这篇关于以编程方式定义新的导航控制器顺序/堆栈?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!