How to present another view controller after dismi

2019-07-29 16:18发布

问题:

How to present another view controller after dismiss from navigation controller in swift ?

I am using Navigation controller.

ViewController1.swift

func pushTo(viewController: UIViewController) {
        let showLocalPwd = self.storyboard?.instantiateViewController(withIdentifier: "LocalPwdVC") as! LocalPwdVC
        self.navigationController?.present(showLocalPwd, animated: true, completion: nil)
}

ViewController2.swift

    @IBAction func btnVerify(_ sender: Any)
    {
            self.dismiss(animated: true, completion: {
                 let vc = self.storyboard.instantiateViewController(withIdentifier: "DataVC") as! DataVC
            self.navigationController.pushViewController(vc, animated: true)
            })
    }

After dismissing the View Controller, it will not goes to next viewcontroller i.e. DataVC

回答1:

If you want to present the view controller then create a protocol in your dismissViewController

protocol dismissViewController {
func presentCompletedViewController()
 }
// Then create a delegate 
   var delegate = dismissViewController? = nil
// If you are using action to dismiss your ViewController then call delegate

     @IBAction func YourButton(_ sender: Any) {
    self.dismiss(animated: true) {
        self.delegate!.presentCompletedViewController()
    }
}

//And then call this in your main or homeViewController
  class HomeViewController: UIViewController, dismissViewController {
 func presentCompletedViewController(){
// enter code to present view controller
 }
 // And at last don't forget to call delegate
    yourVc.delegate = self
    you need to call this delegate in which you are presenting your dismissViewController