Learn Courses My Dashboard

Need to execute a timer reset after a Picker list changes the value of my timer

Here is my situation:

I have a timer in a view with the format minutes:seconds (mm:ss)

The default timer is 1 minute

I have a pick list to allow the timer minutes to be changed, range is 1 - 60.

The timer has to be formatted every time it decreases. This works fine when the timer is running.

The user selects a new minutes from the Pickler list. (before timer is started)
When new minutes are selected I need to reset the timer

***** However I can’t issue this command as part of the Picker list. *****

So right now I have to wait until the timer starts, and at that time it updates to the correct minutes.

I’m struggling with SwiftUI conceptually as far as how and when I can call functions or execute code.

Any help with this particular problem or any resources you can point me to in order to understand better how SwiftUI code execution works would be much appreciated.

I hope I’ve explained this particular problem so that it’s understandable. If not let me know.

Thanks - Ed

You can use an onChange handler to observe the property controlled by the Picker. When it changes, you can fire your reset code.

Something like:

Picker("Timer Duration", $pickerValue) {
    //Picker content
.onChange(of: pickerValue) { _ in

Thank you - that was exactly what I needed. I didn’t know about .onChange - AWESOME!!!