解析中的数据未通过prepareForSegue传递

时间:2019-02-06 11:32:20

标签: swift parse-platform

我想从解析中获取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传递为空。

1 个答案:

答案 0 :(得分:2)

您可以通过执行序列传递数据。只需更改执行以下命令之一:

performSegue(withIdentifier: resultsSegue, sender: objectIdForomParse)

然后在准备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
        }
    }
}

由于您设置了objectIdForomParse而在错误为零时执行segue 仅当您的错误为零时。

更新:

@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
        }
    }
}