导航到ViewController并关闭VC

时间:2019-07-17 05:16:18

标签: ios swift cocoa-touch uiviewcontroller uinavigationcontroller

在这里,我要解散一个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。

3 个答案:

答案 0 :(得分:1)

尝试此代码

@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)
    }
}

答案 1 :(得分:1)

您试图推动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)
    }

答案 2 :(得分:0)

尝试使用以下代码找出导航控制器,并将 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)
})