ios - 如何在不使用 swift 按钮的情况下将数据传递给另一个 ViewController

标签 ios swift uiviewcontroller segue uistoryboardsegue

我目前有这个 Swift 代码,它不会将存储在 Score 中的 int 值传递到 currentScorehigherScore高分currentScorehighScore 均在 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/

相关文章:

iOS:xcode 5,将项目从 xctest 迁移到 senTestingKit

ios - Monotouch AMDeviceInstallApplication 返回 : 0xe8008018

ios - iPhone 核心位置架构 - 如何跨多个 Controller 使用它

ios - Objective-C : Segue back to root from NSObject class

ios - UITextView 取消隐藏后更改 UITableViewCell

iPhone 在呈现模态视图 Controller 时崩溃

ios - 在未运行且 iBeacon 在范围内时调用应用程序

iphone - 如何检测删除按钮 UItableviewcell 是否被隐藏?

ios - 如何使应用程序(使用用户名/密码登录)知道用户已登录?

ios - 在 Swift 中将对象与两个 View Controller 连接