override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
submitTapped()
if let scheduleController = segue.destination as? ScheduleController {
scheduleController.jsonObject = self.info
}
}
在submitTapped()中,爲self.info分配了一個值。但是當我運行我的應用程序時,self.info報告爲「無」。我嘗試在三行中的每一行設置斷點,並且似乎submitTapped()在該函數完成之後才執行。迅速控制流程
這是爲什麼?它是否必須處理線程?我怎樣才能得到submitTapped()執行其餘的?我只是試圖從一個視圖控制器移到另一個視圖控制器,同時也將self.info發送到下一個視圖控制器。
UPDATE:
我最後想出來的(大部分),由於低於+我自己的測試答案。
@IBAction func submitTapped() {
update() { success in
if success {
DispatchQueue.main.async {
self.performSegue(withIdentifier: "showScheduler", sender: nil)
}
}
}
}
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
// I'll probably check the segue identifier here once I have more "actions" implemented
let destinationVC = segue.destination as! ScheduleController
destinationVC.jsonObject = self.info
}
public func update(finished: @escaping (Bool) -> Void) {
...
self.info = jsonObject //get the data I need
finished(true)
...
}
你能展示submitTapped做些什麼,它從哪裏得到它的值? – Scriptable
submitTapped()使用序列化爲JSON的成員變量字符串創建發佈請求,並將其作爲接收JSON的主體,然後將其保存到名爲info([String:AnyObject])的成員變量中。我認爲重要的部分是信息正在被設置在這個函數中,這是與segue相關的唯一部分。你需要我自己發佈代碼嗎? – nuvaryan
@nuvaryan,你可以'執行'繼續什麼時候你會從'json'中得到'閉包'中的值。否則使用'完成'處理程序,如下所示答案 –