-1

So the code I am trying to implement in Swift is based upon this answer here for passing data back from a ViewController: Passing Data with a Callback

Now my issue is after I have called:

self.navigationController?.popViewController(animated: true)

The Prepare For Segue function isn't called in my original View Controller. I assume it shouldn't be called anyway but from that answer I assume there is a possible way to do so?


First View Controller Snippets

override func prepare(for segue: UIStoryboardSegue, sender: Any?) {

    //ignore this segue identifier here, this function works when I am showing a new VC
    if(segue.identifier == "certSegue"){
        let certVC = segue.destination as! CertificateViewController
        certVC.profileModel = profileModel
    }

    //this is what I need to be called
    if(segue.identifier == "dpSegue"){
        print("dpSegue")
        let dpVC = segue.destination as! DatePickerViewController
        dpVC.callback = { result in
            print(result)
            print("Data")
            // do something with the result
        }
        //dpVC.dailyBudgetPassedThrough = "Test"
    }
}

 func showDatePicker(){
    let vc = UIStoryboard.init(name: "Main", bundle: Bundle.main).instantiateViewController(withIdentifier: "DatePickerVC") as? DatePickerViewController
    self.navigationController?.pushViewController(vc!, animated: true)

}

Second View Controller

import UIKit

class DatePickerViewController: UIViewController {

    var callback : ((String)->())?

override func viewDidLoad() {
    super.viewDidLoad()

    // Do any additional setup after loading the view.
}

func sendBackUpdate(){

   print("Callback")
    callback?("Test")
}

@IBAction func cancelButton(_ sender: Any) {
    self.navigationController?.popViewController(animated: true)
}

@IBAction func updateButton(_ sender: Any) {
    sendBackUpdate()
    self.navigationController?.popViewController(animated: true)
}


}
Robert Dresler
  • 10,580
  • 2
  • 22
  • 40
C. Skjerdal
  • 2,750
  • 3
  • 25
  • 50

1 Answers1

0

prepareForSegue is called if in Interface Builder a segue is connected

  • from a table/collection view cell to a destination view controller and the cell is tapped.
  • from a source view controller to a destination view controller and performSegue(withIdentifier:sender:) is called in the source view controller.

It's not called when a view controller is going to be presented with pushViewController

In your case assign the callback after instantiating the controller in showDatePicker, prepare(for segue is not needed.

func showDatePicker(){
    let vc = UIStoryboard(name: "Main", bundle: .main).instantiateViewController(withIdentifier: "DatePickerVC") as! DatePickerViewController
    vc.callback = { result in
        print(result)
        print("Data")
        // do something with the result
    }

    self.navigationController?.pushViewController(vc, animated: true)
}
vadian
  • 274,689
  • 30
  • 353
  • 361
  • Just to confirm understanding, I am popping a view off of my stack and trying to call it afterwards. If you see the link I posted they claim to being passing data back when the controller is removed and the first VC is displayed. – C. Skjerdal Mar 29 '19 at 17:56
  • Right away this just makes sense, I figured my original code seemed odd. Thanks for the updated answer – C. Skjerdal Mar 29 '19 at 19:10