如何在其他导航控制器中弹出视图? [英] How to pop back to a view in a different navigation controller?

查看:82
本文介绍了如何在其他导航控制器中弹出视图?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在我的Swift应用程序中创建以下用户流:

I want to create the following user flow in my Swift application:

  1. 用户单击按钮
  2. 弹出一个模式视图,其中有一个选择.用户选择一个选项.
  3. 出现另一种选择的第二个视图(使用显示"过渡,因此用户可以按< Back 更改选择#1).用户选择第二个选择.
  4. 第二个模式视图切换回原始视图(具有按钮).
  1. User clicks a button
  2. A modal view pops over with a choice. The user selects a choice.
  3. 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.
  4. 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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆