Swiftui Textfield State Change停止使用iOS 15.4.1

发布于 2025-01-22 05:42:44 字数 1875 浏览 0 评论 0原文

直到iOS 15.4.1,以下代码一直运行良好。在输入的值大于100000之后,它只能启用一个文本字段旁边的按钮。它仍然可以与15.3.1一起使用,但停止使用15.4.1。知道什么是为了防止其工作而发生的变化吗?


    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))
        }
    }

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))
        }
    }

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

宣告ˉ结束 2025-01-29 05:42:44

我不确定为什么它以前有效,但是使用选项的适当签名是:

    @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

更改此行:

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

为此

TextField("Work Order Number", value: $vm.woid, format: .number)

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)
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文