如何从 ViewController 5 返回到 ViewController - Swift

How can I go back to ViewController2 from ViewController5 - Swift

在 swift 我有 ViewController1ViewController2ViewController3ViewController4ViewController5,现在从 ViewController1 ,

我使用 performSegue(withIdentifier: "segueIdentifier", sender: nil) 进入 ViewController5,如下所示:

ViewController1 -> ViewController2 -> ViewController3 -> ViewController4 -> ViewController5

现在我在 ViewController5 上,我想转至 ViewController2,我使用 dismiss(animated: true),它只会让你从当前状态退回 1 步ViewController,所以我上了 ViewController4.

问题是,我怎样才能从ViewController5回到ViewController2?

所以当我回到 ViewController2 和 运行 dismiss(animated: true) 我要回到 ViewController1

extension UINavigationController {

func popToViewController<Controller : UIViewController>(_ type: Controller.Type){
    guard let vc = viewControllers.compactMap({ [=10=] as? Controller }).first else { return }
    popToViewController(vc, animated: true)
  } 

}

navigationController?.popToViewController(ViewController.self)