gpt4 book ai didi

ios - 在第二个 View Controller 上加载、保存高分

转载 作者:行者123 更新时间:2023-11-28 12:18:37 25 4
gpt4 key购买 nike

我正在尝试仅在 gameOverViewController 上加载和保存我的高分。我已经成功地将分数从 secondViewController 传输到 gameOverViewController。 gameOverViewController 就像一个普通的游戏结束屏幕,显示您的分数、高分并具有重试和主菜单按钮。我尝试将 highScoreLabel 设置为等于 scoreGameOverLabel,它运行了,但 highScore int 保持为 0 并且不等于 gameOverScore。我正在尝试:

  1. 如果得分>高分,高分=得分
  2. 如果score < high score, high score保持不变

    var addOne = 0
    class SecondViewController: UIViewController
    {

    @IBOutlet weak var score: UITextField!

    score.text = "\(addOne)"

    override func prepare(for segue: UIStoryboardSegue, sender: Any?) {

    let gameOver = segue.destination as! GameOverViewController

    gameOver.gameOverText = score.text!
    gameOver.lastScore = addOne
    }

gameOverViewController

class GameOverViewController: UIViewController {


@IBAction func retryButton(_ sender: Any) {
addOne = 0
highScoreLabel.text = NSString(format: "%i", highScore) as String
}

@IBAction func mainMenuButton(_ sender: Any) {
addOne = 0
highScoreLabel.text = NSString(format: "%i", highScore) as String
}

var lastScore = 0
var highScore = UserDefaults.standard.integer(forKey: "high_score")

@IBOutlet weak var highScoreLabel: UILabel!

@IBOutlet weak var scoreGameOverLabel: UILabel!

var gameOver = ""

override func viewDidLoad() {
super.viewDidLoad()

if lastScore > highScore {
highScore = lastScore
highScoreLabel.text = NSString(format: "%i", highScore) as String
UserDefaults.standard.set(highScore, forKey: "high_score")
}
scoreGameOverLabel.text = gameOver
}

最佳答案

SecondViewController 中,尝试设置您的最后得分。

gameOver.lastScore = addOne

GameOverViewController 中添加 lastScore 变量并跟踪您的高分。

var lastScore = 0
var highScore = UserDefaults.standard.integer(forKey: "high_score")

if lastScore > highScore {
highScore = lastScore
UserDefaults.standard.set(highScore, forKey: "high_score")
}

关于ios - 在第二个 View Controller 上加载、保存高分,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45578105/

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