Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to pass data to another controller on dismiss ViewController?

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?

like image 710
John Doe Avatar asked Apr 30 '17 13:04

John Doe


2 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
  }
}
like image 130
the_legend_27 Avatar answered Oct 10 '22 02:10

the_legend_27


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
    }
}
like image 1
Bence Pattogato Avatar answered Oct 10 '22 03:10

Bence Pattogato