gpt4 book ai didi

ios - prepareForSegue 在 performSegue 之前调用

转载 作者:行者123 更新时间:2023-11-28 12:19:04 24 4
gpt4 key购买 nike

我正在尝试执行一个 segue,将多个变量传递到下一个 View ,包括一个变量 currentID,它是从解析数据库中检索到的。在将 currentID 设置为从数据库下载的 currentID 之前,不应调用 performSegue。但是,当我运行代码时,当 currentID 传递给下一个 View 时,它最终变成了一个空字符串。

这是我的按钮调用的代码:

@IBAction func submitButtonPressed(_ sender: Any) {

let point = PFGeoPoint(latitude:0.0, longitude:0.0)
let testObject = PFObject(className: "Person")

testObject["inputAmount"] = inputAmount
testObject["outputAmount"] = outputAmount
testObject["inputCurrency"] = inputCurrency
testObject["outputCurrency"] = outputCurrency
testObject["location"] = point

testObject.saveInBackground { (success, error) -> Void in

// added test for success 11th July 2016

if success {
print("Object has been saved.")

self.currentID = String(describing: testObject.objectId!)
if(self.currentID != ""){
self.performSegue(withIdentifier: "mainToListSegue", sender: self)
}

} else {
if error != nil {
print (error)
} else {
print ("Error")
}
}
}
}

这里是 prepareForSegue 方法:

override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
let listViewController = (segue.destination as! UINavigationController).viewControllers[0] as! ListViewController
listViewController.inputCurrency = inputCurrency
listViewController.outputCurrency = outputCurrency
listViewController.inputAmount = inputAmount
listViewController.outputAmount = outputAmount
listViewController.currentID = currentID
listViewController.cellContent = cellContent
}

最佳答案

为了满足您的需求,您必须在 View Controller 之间连接您的 segue,而不是从 UIButton 到 View Controller 。

每次你需要在调用之前准备你的 segue 时,程序如下:

enter image description here

然后,命名并使用委托(delegate)方法

override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
if segue.identifier == "mySegue" {

}
}

关于ios - prepareForSegue 在 performSegue 之前调用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45444615/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com