gpt4 book ai didi

swift - 来自解析的数据未通过 prepareForSegue 传递

转载 作者:搜寻专家 更新时间:2023-11-01 06:54:28 25 4
gpt4 key购买 nike

我想从解析中获取 objectId 并通过 segue 传递它。但是 objectId 作为空字符串传递过来:

class QueryViewController: UIViewController {

var objectIdFormParse = String()

@IBAction func makeQueryButtonTapped(_ sender: UIButton) {

makeSearchObject.saveInBackground { (success, error) in
if error == nil {
if let getObjectId = makeSearchObject.objectId {
self.objectIdFormParse = getObjectId
print("objectId in queryVC: \(self.objectIdForomParse)")
}
//Successfully saved
} else {
//Error, check error
}
}
performSegue(withIdentifier: resultsSegue, sender: self)
}

override func prepare(for segue: UIStoryboardSegue, sender: Any?)
{
if segue.identifier == resultsSegue
{
let destination = segue.destination as! ResultsViewController

destination.objectIdFromQueryVC = objectIdForomParse
}
}
}

print 语句正确打印了 objectId,但 segue 为空。

最佳答案

您可以通过 perform segue 传递数据。只需将 perform segue 行更改为以下行:

performSegue(withIdentifier: resultsSegue, sender: objectIdForomParse)

并在您的 prepare for segue 方法中添加以下行:

override func prepare(for segue: UIStoryboardSegue, sender: Any?)
{
if segue.identifier == resultsSegue
{
let destination = segue.destination as! ResultsViewController
let objectIDParse = sender as! String
destination.objectIdFromQueryVC = objectIDParse
}
}
}

当你的错误为零时执行 segue,因为你正在设置 objectIdForomParse仅当您的错误为零时。

更新:

@IBAction func makeQueryButtonTapped(_ sender: UIButton) {

makeSearchObject.saveInBackground { (success, error) in
if error == nil {
if let getObjectId = makeSearchObject.objectId {
self.objectIdFormParse = getObjectId
print("objectId in queryVC: \(self.objectIdForomParse)")
performSegue(withIdentifier: resultsSegue, sender: self)
}
//Successfully saved
} else {
//Error, check error
}
}

}

override func prepare(for segue: UIStoryboardSegue, sender: Any?)
{
if segue.identifier == resultsSegue
{
let destination = segue.destination as! ResultsViewController

destination.objectIdFromQueryVC = objectIdForomParse
}
}
}

关于swift - 来自解析的数据未通过 prepareForSegue 传递,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54552678/

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