iOS Swift 导航通过重定向展开

iOS Swift Navigation unwind with redirection

我想实现以下功能:(我正在使用导航控制器)

视图 A 有几个选项可以确定采用哪条路径。第一个显示视图 B,然后使用导航控制器显示视图 C。工具栏的第一项执行展开到视图 A。这有效。工具栏中的第二项,我不仅要展开到 A,还要重定向到视图 E。

A 视图控制器中的代码如下所示:

@IBAction func unwindToHomeController(segue: UIStoryboardSegue) {
    self.performSegue(withIdentifier: "toPerson", sender: self)
}

当我点击工具栏中的第二项时,显示视图E,但在短暂的延迟后立即显示视图A。

如何停止视图 A 的显示?

也许有更好的方法。

您需要等待动画完成才能执行Segue到E:

class ViewController: UIViewController {

    @IBAction func unwindToA(segue: UIStoryboardSegue) {
    }

    @IBAction func unwindToE(segue: UIStoryboardSegue) {
        CATransaction.begin()
        CATransaction.setCompletionBlock { 
            self.performSegue(withIdentifier: "E", sender: nil)
        }
        CATransaction.commit()
    }

}

UPDATED 为了避免在按下 E

闪烁 显示 A

1) 移除unwindToE函数:

extension ViewController {

    @IBAction func unwindToA(segue: UIStoryboardSegue) {
    }

//  @IBAction func unwindToE(segue: UIStoryboardSegue) {
//      CATransaction.begin()
//      CATransaction.setCompletionBlock { 
//          self.performSegue(withIdentifier: "E", sender: nil)
//      }
//      CATransaction.commit()
//  }

}

2) 创建自定义转场:

class MyUnwindSegue: UIStoryboardSegue {

    override func perform() {

        guard let nav = source.navigationController else { return }
        guard let root = nav.viewControllers.first else { return }
        let viewControllers = [root, destination]
        nav.setViewControllers(viewControllers, animated: true)

    }

}

3) 在情节提要中将转场更新为 MyUnwindSegue(确保为您的项目模块选择了 Module 而不是 empty):