ios - How to pass data to another controller on dismiss ViewController?


Keywords:ios 


Question: 

I want to pass my data from one ViewController to another VC on VC dismiss. Is it possible?

I've tried the next method and no success:

On button click:

self.dismiss(animated: true) { 
        let storyboard = UIStoryboard(name: "Main", bundle: nil)
        let controller = storyboard.instantiateViewController(withIdentifier: "EditViewController") as! EditViewController
        controller.segueArray = [values]
    }

when EditViewController appears again, my segueArray is nil there.

How can I pass my data from my ViewController to the EditViewController on dismiss?


3 Answers: 

The best way to pass data back to the previous view controller is through delegates... when going from ViewController A to B, pass view controller A as a delegate and on the viewWillDisappear method for ViewController B, call the delegate method in ViewController A.. Protocols would help define the delegate and the required methods to be implemented by previous VC. Here's a quick example:

Protocol for passing data:

protocol isAbleToReceiveData {
  func pass(data: String)  //data: string is an example parameter
} 

Viewcontroller A:

class viewControllerA: UIViewController, isAbleToReceiveData {

  func pass(data: String) { //conforms to protocol
  // implement your own implementation
   }

  prepare(for: Segue) {
    /** code for passing data **/
    let vc2 = ViewCOntrollerB()  /
    vc2.delegate = self   //sets the delegate in the new viewcontroller 
                          //before displaying
    present(vc2)
  }
}

Dismissing viewcontroller:

class viewControllerB: UIViewController {

  var delegate: isAbleToReceiveData

  viewWillDisappear {
      delegate.pass(data: "someData") //call the func in the previous vc
  }
}
 

In the dismiss completion block, you create a new instance of the EditViewController. I assume that another EditViewController instance exists back in the navigation stack, you need to find that instance & set the segueArray to values. That you can achieve by iterating through your navigation stack's viewcontrollers like:

viewController.navigationController?.viewControllers.forEach({ (vc) in
    if let editVC = vc as? EditViewController {
        editVC.segueArray = ....
    }
})

But I would recommend to use the delegate pattern, like:

protocol EditViewControllerDelegate: class {
    func setSegueArray(segues: [UIStoryboardSegue])
}

In the viewcontroller (call it just ViewController) where the dismiss block is, declare a delegate property:

class ViewController: UIViewController {
    weak var delegate: EditViewControllerDelegate?
    ....
}

Then on presenting the instance of (I assume from EditViewController) ViewController set the delegate like:

...
if let vc = presentingViewController as? ViewController {
    vc.delegate = self
}

And conform the EditViewController to the delegate protocol like:

extension EditViewController: EditViewControllerDelegate {
    func setSegueArray(segues: [UIStoryboardSegue]) {
        // Do the data setting here eg. self.segues = segues
    }
}
 

To detect when the back button is pressed on a view controller, I just use:

override func didMove(toParentViewController parent: UIViewController?) {
    guard parent == nil else { return } // Back button pressed

    ... // Pass on the info as shown in you example
} // didMoveToParentViewController