0

I have a 3 view controllers, A->B->C but between B and C I have a navigation controller. My C controller is getting some information from the user and I am saving it in Realm(local database). When exiting that controller I want to go to the B controller and show some of that data saved, however, when I try using popToViewController it tells me that either the controller doesn't exist or it just crashes. I have also tried

dismiss(animated: true, completion: nil)

but it only exits the controller and doesn't reload the data on my B controller. Is there a way to use PopToViewController in Swift 4?

This is how I have tried using it:

     let prevVC = self.storyboard?.instantiateViewController(withIdentifier: "FormListVC") as? FormListVC
     navigationController?.popToViewController(prevVC, animated: true)

I have also tried:

for vc in self.navigationController!.viewControllers {
    if vc is FormListVC {
        navigationController?.popToViewController(vc, animated: true)
    }
}

But nothing happens.

Mark
  • 49
  • 6
  • You need to pop to the existing instance of the view controller, not a new instance as you are doing here. Use the `viewControllers` array property of your navigation controller to get the instance. – Paulw11 Jan 07 '20 at 19:38

1 Answers1

1

You are saying when you dismiss(animated: true, completion: nil) then your view controller exits.

This implies your controller is not in the navigation controller but presented modally. So the popToViewController won't work.

If you want to pass data back to your controller after dismiss, use Delegates.

Keshu R.
  • 5,045
  • 1
  • 18
  • 38