【问题标题】:Pop Up ViewController from Navigation Stack从导航堆栈弹出 ViewController
【发布时间】:2018-04-12 06:30:39
【问题描述】:

我正在学习来自 Apple Developer 的教程:Start Developing iOS Apps (Swift),我对推送和模态搜索感到困惑。

有两个场景,导航栏的保存和取消按钮,从场景2回到场景1。

如果取消按钮被按下,它将调用不同的方法来关闭场景 2:

@IBAction func cancel(_ sender: UIBarButtonItem) {
    // Depending on style of presentation (modal or push presentation), this view controller needs to be dismissed in two different ways.
    let isPresentingInAddMealMode = presentingViewController is UINavigationController

    if isPresentingInAddMealMode {
        dismiss(animated: true, completion: nil)
    }
    else if let owningNavigationController = navigationController{
        owningNavigationController.popViewController(animated: true)
    }
    else {
        fatalError("The MealViewController is not inside a navigation controller.")
    }
}

在这个方法中,如果场景是通过modal segue呈现的,就调用dismiss(animated:completion:),并且 如果场景是通过 push segue 呈现的,则调用 popViewController(animated:) 从导航堆栈中弹出 ViewController。

但是对于 Save 按钮,本教程重写了场景 2 中的方法 prepare(for:sender:) 和场景 1 中的操作方法 unwindToMealList(sender:)。 并将保存按钮拖到退出(场景停靠中的按钮)并选择 unWindToMealList(sender:) 方法。 所以流程将是:prepare(for:sender:) -> 场景 2 被取消,场景 1 呈现 -> unWindToMealList(sender:)

我想知道当按下 Save 按钮时,代码 sn-ps 并没有明确地关闭场景 2 并删除导航堆栈中的 ViewController。 我知道模态 segue 不会将 ViewController 推送到导航堆栈,但 push segue 会推送它。 为什么代码 sn-ps 没有从导航栈中弹出?

非常感谢。

【问题讨论】:

    标签: ios iphone swift uiviewcontroller


    【解决方案1】:

    您正在阅读的教程似乎使用了unwind segues

    展开转场,就像普通转场一样,有一个源和一个目标,你可以在prepareForSegue 中准备它,但是它不会显示目标 VC,它会关闭源 VC,以便显示目标 VC。

    Unwind segues 在不同的情况下表现不同。当您使用 push segue 从 VC A 呈现 VC B 并从 B 到 A 的展开 segue 时,展开 segue 将从导航堆栈中弹出 VC B。当您从 VC A 模态呈现 VC B 时,展开转场将关闭模态呈现的 VC。

    如您所见,展开转场非常聪明。它将自行决定如何显示目标 VC。它甚至可以在导航堆栈中弹出两个或多个 VC!

    【讨论】:

    • 知道了。谢谢!
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2014-08-05
    • 2015-05-31
    • 1970-01-01
    • 2013-02-19
    • 2020-04-12
    • 2023-03-22
    • 1970-01-01
    相关资源
    最近更新 更多