如何在其他导航控制器中弹出视图? [英] How to pop back to a view in a different navigation controller?
问题描述
我想在我的Swift应用程序中创建以下用户流:
I want to create the following user flow in my Swift application:
- 用户单击按钮
- 弹出一个模式视图,其中有一个选择.用户选择一个选项.
- 出现另一种选择的第二个视图(使用显示"过渡,因此用户可以按< Back 更改选择#1).用户选择第二个选择.
- 第二个模式视图切换回原始视图(具有按钮).
- User clicks a button
- A modal view pops over with a choice. The user selects a choice.
- A second view appears with another choice (using a "show" transition, so the user can press < Back to alter choice #1). The user selects the second choice.
- The second modal view segues back to the original view (which has the button).
为此,我创建了以下结构:
To do this, I created the following structure:
--> NC1 --> VC1 --modal-> NC2 --> VC2 --show-> VC3 (NC = UINavigationController,
^ | VC = UIViewController)
| |
----Segue I'm trying to achieve-----
我一直尝试通过将对VC1的引用一直传递到VC3,然后在VC3中调用以下代码来将其一直弹出到VC1:
I've tried to pop all the way back to VC1 by passing a reference to VC1 all the way through to VC3, then calling the following code in VC3:
override func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
// Perform Segue
print("Trying to segue")
rootController!.navigationController!.popToRootViewControllerAnimated(true)
tableView.deselectRowAtIndexPath(indexPath, animated: true)
}
其中,rootController
是对VC1的引用.不幸的是,这种情况没有发生.第二个模式视图仍保留在屏幕上,但是我得到了正在尝试隐藏"消息.
where rootController
is the reference to VC1. Unfortunately the segue doesn't occur; the second modal view stays on the screen, but I get the "Trying to segue" message printed.
我要尝试以正确的方式实施此用户流程吗?如果是这样,为什么我不能在另一个导航控制器中弹出根视图?
Am I trying to implement this user flow the right way? If so, any suggestions why I can't pop to a root view in another navigation controller?
推荐答案
如果您使用的是情节提要,则可以使用展开键来实现.
If you are using storyboards, you can do this using an unwind segue.
在VC1中实现@IBAction
方法,该方法将UIStoryboardSegue
作为参数:
In VC1 implement an @IBAction
method, which takes a UIStoryboardSegue
as parameter:
@IBAction func unwindToVC1(sender: UIStoryboardSegue) {
}
然后从VC3的Prototype单元中,拖动至视图控制器的退出"图标,选择Selection Segue: unwindToVC1:
,您就可以开始了.
Then from the Prototype cell of VC3 control-drag to the Exit icon of the view controller, select Selection Segue: unwindToVC1:
and you are good to go.
您也可以使用代码获得类似的结果.
像这样在VC3中实现didSelectRowAtIndexPath
:
You can achieve a similar result using code also.
Implement didSelectRowAtIndexPath
in VC3 like this:
override func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
navigationController!.dismissViewControllerAnimated(true, completion: nil)
}
这篇关于如何在其他导航控制器中弹出视图?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!