Search code examples
swifttimerlabel

updating label variables from a running timer


I'm trying to create a counter that does simple math when the timer reaches zero. I have my timer working somewhat correctly, and the labels displaying the original variables, but they are not updating as the timer hits "zero". Where am I going wrong in my code?

    class ViewController: UIViewController {


    @IBOutlet weak var timerLabel: UILabel!
    @IBOutlet weak var goldCounter: UILabel!
    @IBOutlet weak var turnCounter: UILabel!

    var seconds = 15
    var timer = Timer()
    var gold = 1000
    var turns = 1



    func updatelabels () {
    goldCounter.text = String(gold)
    turnCounter.text = String(turns) }




    func runTimer() {
        timer = Timer.scheduledTimer(timeInterval: 1, target: self, selector: #selector(updateTimer),userInfo: nil, repeats: true)
    }

    @objc func updateTimer() {
        seconds -= 1
        timerLabel.text = String(seconds)
        if seconds == 0 {
            seconds = 15}
        }


    func increaseGold () {
        if seconds == 1 {
            gold = gold + 1000
            turns = turns + 1
        }}

override func viewDidLoad() {
        super.viewDidLoad()
        self.runTimer()
        goldCounter.text = String(gold)
        turnCounter.text = String(turns)
        // Do any additional setup after loading the view, typically from a nib.

    func increaseGold () {
        if seconds == 1 {
            gold = gold + 1000
            turns = turns + 1
        }}
   func updatelabels () {
        goldCounter.text = String(gold)
        turnCounter.text = String(turns) }

    }
}

Solution

  • Your main issue is that you aren't calling updateLabels to update your labels.

    I would suggest using property observers (didSet) to set your labels as the values change instead of relying on a separate function to change them.

    Also, you need to call increaseGold in your timer handler:

    class ViewController: UIViewController {
    
        @IBOutlet weak var timerLabel: UILabel!
        @IBOutlet weak var goldCounter: UILabel!
        @IBOutlet weak var turnCounter: UILabel!
    
        var seconds = 15 {
            didSet {
                timerLabel.text = String(seconds)
            }
        }
    
        var timer = Timer()
    
        var gold = 1000 {
            didSet {
                goldCounter.text = String(gold)
            }
        }
    
        var turns = 1 {
            didSet {
                turnCounter.text = String(turns)
            }
        }
    
        func runTimer() {
            timer = Timer.scheduledTimer(timeInterval: 1, target: self, selector: #selector(updateTimer),userInfo: nil, repeats: true)
        }
    
        @objc func updateTimer() {
            seconds -= 1
            if seconds == 0 {
                seconds = 15
            }
            increaseGold()
        }
    
        func increaseGold () {
            if seconds == 1 {
                gold = gold + 1000
                turns = turns + 1
            }
        }
    
        override func viewDidLoad() {
            super.viewDidLoad()
            seconds = 15
            gold = 1000
            turns = 1
            self.runTimer()
        }
    }