有没有办法可以从我的闭包中返回shouldPerformSegueWithIdentifier
方法的值?或者我应该采取不同的方式吗?
override func shouldPerformSegueWithIdentifier(identifier: String?, sender: AnyObject?) -> Bool {
getDataFromSomewhere() { succeeded, data in
if succeeded {
// Should return true here
} else {
self.errorAlert("Error", message: "Can't get data...")
// Should return false here
}
}
}
答案 0 :(得分:2)
您无法从放置shouldPerformSegueWithIdentifier:sender:
的位置返回// Should return true here
。你需要做这样的事情:
override func shouldPerformSegueWithIdentifier(identifier: String?, sender: AnyObject?) -> Bool {
var returnValue: Bool = false
getDataFromSomewhere() { succeeded, data in
if succeeded {
returnValue = true
} else {
self.errorAlert("Error", message: "Can't get data...")
returnValue = false
}
}
return returnValue
}
请注意,这仅在getDataFromSomewhere
在getDataFromSomewhere
返回之前执行闭包时才有效。如果getDataFromSomewhere
存储闭包,并安排稍后调用它(例如,在异步网络请求之后),那么这将不起作用。它只会使shouldPerformSegueWithIdentifier:sender:
返回您用于初始化returnValue
的值。
答案 1 :(得分:0)
您是否反对使用prepareforsegue和unwind segue?
override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
if segue.identifier == "fuelButtonUnhide"{
if let vc = segue.destinationViewController as? DateViewController {
vc.fuelHidden = false
}
}
}
@IBAction func unwindDateViewToAddFuel(segue: UIStoryboardSegue){
//Change any data from the date view here
println("Hit done from date view")
}
您可以在prepareforsegue中传递数据并在展开操作中检索数据。