【发布时间】:2019-03-29 17:03:33
【问题描述】:
所以我试图在 Swift 中实现的代码是基于这里的答案,用于从 ViewController 传回数据: Passing Data with a Callback
现在我的问题是在我打电话之后:
self.navigationController?.popViewController(animated: true)
Prepare For Segue 函数未在我的原始视图控制器中调用。我认为无论如何都不应该调用它,但是从那个答案中我认为有一种可能的方法吗?
第一个视图控制器片段
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)
}
}
【问题讨论】: