1

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 Answers1

2

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
Wings
  • 2,398
  • 23
  • 46
  • where you getting the problem – Wings Apr 02 '18 at 15:43
  • you need to call this function in which your calling your dismiss view controller let vc = self.storyboard?.instantiateViewController(withIdentifier: "dismissVC") as! dismissViewController vc.delegate = self – Wings Apr 02 '18 at 15:47
  • self.dismiss(animated: true, completion: { self.delegate!.presentVisitorDataVC() }) . .... It shows "Unexpectedly found nil while unwrapping an optional value". – Creative Study Apr 02 '18 at 15:47
  • its because your are not calling delegate method go to your firstcontroller from which you called the dismiss view controller and add this in your push segue – Wings Apr 02 '18 at 15:48
  • Let us [continue this discussion in chat](https://chat.stackoverflow.com/rooms/168056/discussion-between-v-rohit-and-creative-study). – Wings Apr 02 '18 at 15:49