Итак, код, который я пытаюсь реализовать в Swift, основан на этом ответе здесь для передачи данных обратно из ViewController: Передача данных с обратным вызовом
Теперь моя проблема после того, как явызвали:
self.navigationController?.popViewController(animated: true)
Функция Prepare For Segue не была вызвана в моем исходном View Controller.Я предполагаю, что это не должно вызываться в любом случае, но из этого ответа я предполагаю, что есть возможный способ сделать это?
Фрагменты первого вида контроллера
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)
}
Контроллер второго вида
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)
}
}