How to pass data to another controller on dismiss ViewController?
ios, storyboard, swift, uistoryboardsegue, uiviewcontroller
Solution
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
}
}
Problem
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?