简体   繁体   English

在第二个视图控制器上加载并保存高分

[英]Load, save high score on second view controller

I am trying to load and save my high score on only the gameOverViewController. 我正在尝试仅在gameOverViewController上加载并保存我的高分。 I have successfully transferred the score from the secondViewController to the gameOverViewController. 我已经成功将分数从secondViewController转移到gameOverViewController。 The gameOverViewController is like a normal game over screen that shows your score, high score and has retry and main menu button. gameOverViewController就像普通的游戏屏幕一样,可以显示您的得分,高分,并具有重试和主菜单按钮。 I tried to set the highScoreLabel equal to the scoreGameOverLabel, it runs, but the highScore int stays at 0 and doesn't equal the gameOverScore. 我试图将highScoreLabel设置为scoreGameGameLabel,它运行,但是highScore int保持为0,不等于gameOverScore。 I am trying to: 我在尝试着:

  1. if score > high score , high score = score 如果得分>高分,高分=得分
  2. if score < high score, high score remains same 如果分数<高分,则高分保持不变

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

Firstly , 首先

highScoreLabel = scoreGameOverLabel

Should probably be 应该是

 highScoreLabel.text = scoreGameOverLabel.text

Secondly, as per your snippet, in GameOverViewController there is no addOne , how are you setting it and using it to compare in If condition ? 其次,按照您的代码段,在GameOverViewController中没有addOne ,您如何设置它并使用它在If条件中进行比较?

In SecondViewController , try setting your last score. SecondViewController ,尝试设置您的最后得分。

gameOver.lastScore = addOne

In GameOverViewController add the lastScore variable and keep track of your high score. 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")
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM