导航至 ViewController 并关闭 VC

Navigate to ViewController and dissmiss VC

我在这里关闭一个 VC 并尝试导航新的 VC,但导航不起作用。关闭 VC 工作正常。

@IBAction func onClickEmailBtn(_ sender: UIButton) {
    //dismiss VC
    self.dismiss(animated: false, completion: nil)
    //Navigate to VC
    DispatchQueue.main.async {
        let cevc = self.storyboard?.instantiateViewController(withIdentifier: "CEVC") as! EmailViewController
        self.navigationController?.pushViewController(cevc, animated: true)
    }
}

这里我有一个名为 VC1 的 NavigationViewController,在此基础上我展示了一个名为 VC2 的 VC。在这个 VC2 中,当我单击按钮时,我想导航名为 EmailVC.

的新 VC

尝试使用以下代码找出导航控制器,并将 Viewcontroller 替换为您的控制器名称。

let productVC = SeconViewViewController()
let parentVC = (parent!.presentingViewController as! ViewController).childViewControllers.first as! UINavigationController
self.navigationController?.dismiss(animated: true, completion: {
     parentVC.pushViewController(productVC, animated: true)
})

试试这个代码

@IBAction func onClickEmailBtn(_ sender: UIButton) {

    if let controller = self.storyboard?.instantiateViewController(withIdentifier: "CEVC") as! EmailViewController {
         self.dismiss(animated: false, completion: nil)
         self.presentingViewController?.present(controller, animated: true, completion: nil)
    }
}

您正试图推动 ViewVontroller 退出视图控制器, 并且您接受的答案是提出 viewController 而不是驳回 viewController。虽然它可能符合你的目的,但我还是要回答你原来的问题

   // get the object of presenting nanvigation controller(navigation controller with has presented this view controller)
    let presentingNavigationController = presentingViewController?.navigationController
    dismiss(animated: true) {
        let cevc = self.storyboard?.instantiateViewController(withIdentifier: "CEVC") as! EmailViewController
        // here you push your view controller with presentingNavigationController
        presentingNavigationController?.pushViewController(cevc, animated: true)
    }