I'm new to iOS and Swift development, and I'm looking for help with a practice application.
It's a super simple game where you move a slider to "guess" a target/random number, and you are either correct or incorrect. The game itself runs and play works, but I ran into issues when I tried to add a "Play Again" button to reset the UI for a new game.
The issue I'm seeing is when I click the "Play Again?" button, the UI does not update. I'm trying to hide labels and change messages, but the UI does not change like it does when the game plays.
Does anyone know what mistake I'm making that's resulting in not having a UI update when I click "Play Again?" (linked to the reset() function). For reference, I'm using Xcode 10.0 and Swift 4.2.
Thanks!
Pictures of Boring Game
ViewController
import UIKit
class ViewController: UIViewController {
var targetNumber = 0
@IBOutlet weak var message: UILabel!
@IBOutlet weak var slider: UISlider!
@IBOutlet weak var resultLabel: UILabel!
@IBOutlet weak var playAgainBtn: UIButton!
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
playAgainBtn.isHidden = true
targetNumber = Int(arc4random_uniform(101))
message.text = "Move slider to: \(targetNumber)"
}
func correct() {
resultLabel.text = "Bullseye!"
resultLabel.backgroundColor = UIColor.green
}
func wrong() {
resultLabel.text = "Whoops! Not very good at this, are you?"
resultLabel.backgroundColor = UIColor.red
}
func scoreInRange() -> Bool {
let allowed_error = 3
return Int(slider.value) >= Int(targetNumber) - allowed_error && Int(slider.value) <= Int(targetNumber) + allowed_error
}
@IBAction func reset(_ sender: Any) {
slider.setValue(50.0, animated: false)
targetNumber = Int(arc4random_uniform(101))
message.text = "Move slider to: \(targetNumber)"
resultLabel.isHidden = true
playAgainBtn.isHidden = true
}
@IBAction func score(_ sender: Any) {
if scoreInRange() {
correct()
} else {
wrong()
}
playAgainBtn.isHidden = false
}
}


