I have a button which I want to disable when my timer ends in Xcode but to disable to button it needs to be an outlet. However, I need it to be an action because it's an important button.
Code:
import UIKit
class ViewController: UIViewController {
var count = 0
var timer = 60
var highScore = 0
@IBOutlet weak var output: UILabel!
@IBOutlet weak var timerText: UILabel!
@IBOutlet weak var scoreText: UILabel!
@IBOutlet weak var highscoreText: UILabel!
@IBAction func buttonOne(_ sender: Any) {
count += 1
output.text = String(count)
}
@IBAction func StartButton(_ sender: Any) {
Timer.scheduledTimer(timeInterval: 1.0, target: self, selector: Selector(("updateCounting")), userInfo: nil, repeats: true)
}
@objc func updateCounting(){
if timer > 0 {
timer -= 1
timerText.text = String(timer)
} else {
buttonOneOutlet.isEnabled = false
timerText.text = "Time up!"
scoreText.text = "Score: " + String(count)
if count > highScore {
highScore = count
highscoreText.text = String(count)
} else {
highscoreText.text = String(highScore)
}
}
}
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view.
}
}