1

The following code has worked fine until iOS 15.4.1. It simply enables a button next to a textfield after the value entered is greater than 100000. It still works with 15.3.1 but stopped working with 15.4.1. Any idea what was changed to prevent it from working?


    import SwiftUI
    
    struct ContentView: View {
        @State private var woid:Int? = nil
        @State var showResults = false
        private var woidFormatter:NumberFormatter = {
            let formatter = NumberFormatter()
            formatter.numberStyle = .decimal
            formatter.groupingSeparator = ""
            return formatter
        }()
        
        var body: some View {
            Form {
                Section(header: Text("Work Order Lookup")) {
                    HStack {
                        TextField("Work Order Number", value: $woid, formatter: woidFormatter)
                            .keyboardType(.numberPad)
                        Spacer()
                        
                        Button("Lookup") {
                            
                            showResults.toggle()
                        }.buttonStyle(PurpleButton(disabled: (woid ?? 0) < 100000))
                            .disabled((woid ?? 0) < 100000)
                    }
                }
            }
        }
    }
    
    struct ContentView_Previews: PreviewProvider {
        static var previews: some View {
            ContentView()
        }
    }
    
    struct PurpleButton: ButtonStyle {
        let disabled:Bool
        let cardColor:Color = Color(#colorLiteral(red: 0.5568627715, green: 0.3529411852, blue: 0.9686274529, alpha: 1))
        
        func makeBody(configuration: Configuration) -> some View {
            configuration.label
                .padding()
                .background(disabled ? Color.gray : cardColor)
                .foregroundColor(.white)
                .clipShape(RoundedRectangle(cornerRadius: 10, style: .continuous))
        }
    }

Vadim Kotov
  • 8,084
  • 8
  • 48
  • 62
ShouldHaveBeen
  • 631
  • 1
  • 6
  • 10
  • This needs a [Minimal Reproducible Example (MRE)](https://stackoverflow.com/help/minimal-reproducible-example). There is too much code we can't see to be able to help. – Yrb Apr 18 '22 at 20:12
  • @Yrb I added enough code to be able to replicate the problem. – ShouldHaveBeen Apr 18 '22 at 20:58
  • iam curious, the sample code should work, also with the optional --> default value. – saro Apr 18 '22 at 21:27
  • That code has worked since last November and it still works in the previous version of iOS. – ShouldHaveBeen Apr 18 '22 at 21:28
  • yes i agree, whats the error message – saro Apr 18 '22 at 21:31
  • @Yrb the work order is an Int. I tested it by removing the optional and you're right it works but it was working with an optional before the iOS update. I'll try changing it to a string, converting it to an int, and then making the comparison. Am I to assume it was a bug before? – ShouldHaveBeen Apr 18 '22 at 22:17
  • @saro. No error message. It just was't updating the control state and therefore the button would never enable. See fix below – ShouldHaveBeen Apr 18 '22 at 23:10

1 Answers1

0

I'm not sure why it worked before but the proper signature for using optionals is this one:

    @available(iOS 15.0, macOS 12.0, tvOS 15.0, watchOS 8.0, *)
    public init<F>(_ titleKey: LocalizedStringKey, value: Binding<F.FormatInput?>, format: F, prompt: Text? = nil) where F : ParseableFormatStyle, F.FormatOutput == String

Changing this line:

TextField("Work Order Number", value: $vm.woid, formatter: woidFormatter)

to this works

TextField("Work Order Number", value: $vm.woid, format: .number)
ShouldHaveBeen
  • 631
  • 1
  • 6
  • 10