I am making a game in Swift that records the high score and initials (similar to pinball records) and displays it on the game screen as labels. I am using the code below to record the high score and initials on my phone and update them. For the time being, this program will only be on my phone so I am not concerned about storing or updating data on a remote database.
What I don't know how to do (yet) is make a little popup asking the user to enter their initials on a keyboard when the quit button is pressed, if they have the current high score. Ideally, I want it to be able to only accept 3 characters and to update the initials label immediately, before the user is taken to a different view controller.
@IBOutlet weak var scoreLabel: UILabel!
@IBOutlet weak var highScoreLabel: UILabel!
@IBOutlet weak var highScoreInitialsLabel: UILabel!
var score : Int = 0
//Stores and sets high score initials
var oldHighScoreInitials : String = "AAA"
var highScoreInitials : String {
get {
return UserDefaults.standard.string(forKey: "highScoreInitials") ?? "AAA"
}
set {
UserDefaults.standard.set(newValue, forKey: "highScoreInitials")
}
}
//Stores and sets high score
var oldHighScore : Int = 0
var highScore : Int {
get {
return UserDefaults.standard.integer(forKey: "highScore")
}
set {
UserDefaults.standard.set(newValue, forKey: "highScore")
}
}
override func viewDidLoad() {
super.viewDidLoad()
//Updates high score and initials labels with stored highest score and associated initials
highScoreLabel.text = String(highScore)
highScoreInitialsLabel.text = String(highScoreInitials)
oldHighScore = highScore
oldHighScoreInitials = highScoreInitials
}
//Asks for initials if new high score, and segues to Main VC
@IBAction func quitButtonPressed(_ sender: AnyObject) {
if (score > highScore){
highScore = score
print("Ask for initials")
print("Game over, thanks for playing!"
print("Segue to Main VC")
}
else {
print("Game over, thanks for playing!")
print("Segue to Main VC")
}
}
Thank you very much to anybody that offers assistance or advice.