infernouk infernouk - 23 days ago 11
Swift Question

Counter wont subtract on button press?

I am trying to get my setCount to -1 each time the endSetPressed action is called.

I have included setCount -= 1 to the updateTimer function so that when endSetPressed is pressed, it should -1 from the setCount and then start the restCount timer. However in practice it doesnt seem to show it doing the -1 in the app it just stays fixed at the starting setCount value.

Another issue was that I want to be able to see the value of restRemainingCountdownLabel as the user is setting the restStepperValueChanged value, i figured this was done via 'restRemainingCountdownLabel.text = String(restCount)' however as im using 'restCount = Int(sender.value)*60' to generate a value in minutes, its showing the restRemainingCountdownLabel in seconds rather than mins, Appreciate some guidance on that one too!

Here is my code:

@IBOutlet weak var restRemainingCountdownLabel: UILabel!
@IBOutlet weak var setsRemainingCountdownLabel: UILabel!
@IBOutlet weak var numberOfSetsLabel: UILabel!
@IBOutlet weak var numberOfRestLabel: UILabel!
@IBOutlet weak var adjustSetsStepper: UIStepper!
@IBOutlet weak var adjustRestStepper: UIStepper!

var restTimer: Timer?
var restCount = 0
var setCount = 0

@IBAction func endSetPressed(_ sender: Any) {
restTimer = Timer.scheduledTimer(timeInterval: 1.0, target: self, selector: #selector(RestController.updateTimer), userInfo: nil, repeats: true)
}

@IBAction func setStepperValueChanged(_ sender: UIStepper) {
numberOfSetsLabel.text = Int(sender.value).description
setCount = Int(sender.value)
setsRemainingCountdownLabel.text = String(setCount)
}

@IBAction func restStepperValueChanged(_ sender: UIStepper) {
numberOfRestLabel.text = Int(sender.value).description
restCount = Int(sender.value)*60
restRemainingCountdownLabel.text = String(restCount)
}

@IBAction func resetSetsButton(_ sender: Any) {
//setCount = Int
}

override func viewDidLoad() {
super.viewDidLoad()

}

func updateTimer() {
if (setCount > 0){
setCount -= 1
}
if (restCount > 0){
let minutes = String(restCount / 60)
let seconds = String(restCount % 60)
restRemainingCountdownLabel.text = minutes + ":" + seconds
restCount -= 1
}
}

Answer

Have a look at your updateTimer() method, first line:

if (setCount > 0)

if setCount > 0 then subtract 1 from setCount

but...here you've declared setCount with a value of 0

var setCount = 0

So, you never end up in your if part :)

How to Debug This

The next time you have a problem like this, try adding some print() statements to your code.

In your case you could do something like this:

@IBAction func endSetPressed(_ sender: Any) {
    print("endSetPressed")
    restTimer = Timer.scheduledTimer(timeInterval: 1.0, target: self, selector: #selector(RestController.updateTimer), userInfo: nil, repeats: true)
}

@IBAction func setStepperValueChanged(_ sender: UIStepper) {
    numberOfSetsLabel.text = Int(sender.value).description
    setCount = Int(sender.value)
    setsRemainingCountdownLabel.text = String(setCount)
}

@IBAction func restStepperValueChanged(_ sender: UIStepper) {
    numberOfRestLabel.text = Int(sender.value).description
    restCount = Int(sender.value)*60
    restRemainingCountdownLabel.text = String(restCount)
}

@IBAction func resetSetsButton(_ sender: Any) {
   //setCount = Int
}

override func viewDidLoad() {
    super.viewDidLoad()
}

func updateTimer() {
    print("updateTimer")
    if (setCount > 0){
        print("setCount > 0")
        setCount -= 1
        print("setCount is now \(setCount)")
    }
    if (restCount > 0){
        print("restCount > 0")
        let minutes = String(restCount / 60)
        let seconds = String(restCount % 60)
        restRemainingCountdownLabel.text = minutes + ":" + seconds
        restCount -= 1
        print("restCount is now \(restCount) - minutes: \(minutes) - seconds: \(seconds)")
    }
}

That should give you some indications about what is going on and the "path" your code follows.

Hope that helps you.

Comments