我目前有这个 Swift 代码,它不会将存储在 Score 中的 int
值传递到 currentScore
和 higherScore
到 高分
。 currentScore
和 highScore
均在 GameOverViewController
上实例化。
override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
if segue.identifier == "GameOverVC" {
let svc: GameOverViewController = segue.destinationViewController as! GameOverViewController
score = svc.currentScore
higherScore = svc.highScore
}
}
我目前正在使用 Storyboard推送到下一个 View 。这是推送代码。
if let resultController = self.storyboard?.instantiateViewControllerWithIdentifier("GameOverVC") as? GameOverViewController {
self.presentViewController(resultController, animated: true, completion: nil)
}
所有代码都可以正常编译,所以我是否需要有一个实际的 UIButton
或者是否需要添加其他内容到方法或 GameOverViewCotroller
中?
最佳答案
您分配值的方式是错误的...
override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
if segue.identifier == "GameOverVC" {
let svc: GameOverViewController = segue.destinationViewController as! GameOverViewController
svc.currentScore = score //pass self.score to svc.currentScore
svc.highScore = higherScore //pass self.higherScore to svc.currentScore
}
}
关于ios - 如何在不使用 swift 按钮的情况下将数据传递给另一个 ViewController,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35098262/