Thursday, June 4, 2026
HomeiOS Developmentios - SwiftUI picker worth to reset if it goes past max

ios – SwiftUI picker worth to reset if it goes past max

[ad_1]

I’ve a customized SwiftUI picker. The picker takes two issues: hour and minute. My struct has a worth referred to as maxDurationSeconds. My objective is, if the person selects a mix of hour and minute, which makes the general go above the maxDurationSeconds, I wish to reset each hour and minute to zero.

var physique: some View {
    let hours = [Int](0...maxHours)
    let minutes = [Int](0...maxMinutes)
    GeometryReader { geometry in
        HStack(spacing: 0) {
            Picker(choice: self.choice.hours, label: Textual content("")) {
                ForEach(0..<maxHours, id: .self) { index in
                    Textual content("(hours[index]) hr")
                        .foregroundColor(Coloration(Asset.Coloration.V2.white.shade))
                }
            }
            .pickerStyle(.wheel)
            .body(width: geometry.measurement.width / 2, top: geometry.measurement.top, alignment: .heart)
            Picker(choice: self.choice.minutes, label: Textual content("")) {
                ForEach(0..<maxMinutes, id: .self) { index in
                    Textual content("(minutes[index]) min")
                        .foregroundColor(Coloration(Asset.Coloration.V2.white.shade))
                }
            }
            .pickerStyle(.wheel)
            .body(width: geometry.measurement.width / 2, top: geometry.measurement.top, alignment: .heart)
        }
    }
}

The remainder of the struct is omitted because the file is big. However right here is the required information:
We’re getting a binding from choice, which has hour and minute in it.
MaxHours and maxMinutes are 24 and 60.

That is what I’m attempting to attain:

    let hoursInSeconds = choice.hours.wrappedValue
    let minutesInSeconds = choice.minutes.wrappedValue
    if(hoursInSeconds + minutesInSeconds > Int(maxDurationSeconds)) {
        choice.hours.wrappedValue = 0
        choice.minutes.wrappedValue = 0
    }

I’m not positive the place to incorporate this code. I attempted doing on faucet gesture of the picker views and doing this, however it might not replace them. Any suggestions is drastically appreciated.

[ad_2]

RELATED ARTICLES

LEAVE A REPLY

Please enter your comment!
Please enter your name here

Most Popular

Recent Comments