0

I have 3 ViewControllers A B C. A and B ViewControllers use C to get data. Both A and B have a select button so if user is on A ViewController and presses select a segue is performed to C the user selects data that is stored in a dictionary pressed done and segue is performed to come back to A and dictionary is sent back using prepare for segue. Exactly the same if user is on B ViewController.

Problems How do I let C know which ViewController to send the dictionary back to. Is their a way I can use dismiss to send the dictionary back to the ViewController the user came from or maybe a better way.

Code from C ViewContrller

@IBAction func doneBtnPressed(_ sender: Any) {
    performSegue(withIdentifier: "backToAddMeetingVC", sender: self)

    //Cant work cuz cant perform 2 segues at once
    performSegue(withIdentifier: "backToProjectVC", sender: <#T##Any?#>)
}

override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
    if segue.identifier == "backToAddMeetingVC" {
        let destination = segue.destination as! AddMeetingVC
        destination.selectedMembers = self.selectedMembers
    }

    if segue.identifier == "backToProjectVC" {
        let destination = segue.destination as! ProjectsVC
        destination.selectedMembers = self.selectedMembers

    }
}
Aman Chawla
  • 229
  • 3
  • 12
  • I think you want to implement a delegate. [Here is how](https://stackoverflow.com/q/40501780/1457385). – shallowThought Jun 28 '17 at 08:47
  • Refer this https://stackoverflow.com/questions/20625660/passing-data-between-2-uiviewcontroller-using-delegate-and-protocol. You will find your answer – Moin Shirazi Jun 28 '17 at 09:02

3 Answers3

2

In C controller create

var dismissClosure: ((neededData) -> Void)?

In controller which presented C its controller(B) in prepareSegue need to declare this:

override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
    let controller = segue.destination as! C
    controller.dismissClosure = { [weak self] neededData in
        guard let `self` = self else { return }
        //TODO YOUR CHANGES
    }
}

When you dismiss or pop controller call this block in C controller

dismiss:

dismiss(true) {
   dismissClosure?(neededData)
}

pop

dismissClosure?(neededData)
navigationController?.popViewController(animated: true)
  • I solved that but now Im getting 2 new errors "Unable to insert COPY_SEND" and "Unable to deallocate send right" also the dismissClosure goes empty it is not picking up the neededData – Aman Chawla Jun 28 '17 at 11:25
  • Perhaps you are working with the UI not in the main thread [Example](https://stackoverflow.com/questions/39576314/dealloc-a-viewcontroller-warning). And of course in block use only weak self ref (I changed post) – Sergei Navka Jun 29 '17 at 06:42
  • Ok i don't understand your changes all Im trying to do is get dictionary from C to a Dictionary in B on dismissing C – Aman Chawla Jun 29 '17 at 07:17
  • In C controller create optional closure, in B controller in "prepare(for segue)" method declare this closure, because before appearing C controller called "prepare(for segue)" and initializate this closure When you dismiss or pop C controller, call closure and put your dictionary in brackets. The result from C will come to B Controller immediately, in B controller where you declare closure, and manipulate with your dictionary. *VERY IMPORTANT* in callback closure use weak self reference, not strong. And when you make changes in UI like pop and dismiss make it in main thread. – Sergei Navka Jun 29 '17 at 10:09
0

Inside doneBtnPressedeither use below if you are not using navigationController

self.dismiss(animated: true, completion: nil)

OR

use below line if you are using navigationController

self.navigationController?.popViewController(animated: true)

prepare for segue looks fine

Kamran
  • 14,987
  • 4
  • 33
  • 51
0

You can use a global variable name like mySelectedViewController and sets its value whenever button action is performed, like when you click from ViewController A set mySelectedViewController = viewA and then in C ViewController check mySelectedViewController value and work according to it.

Amal T S
  • 3,327
  • 2
  • 24
  • 57
Rajeev Singh
  • 198
  • 1
  • 11