SwiftUI:如何触发 onSubmit 修饰符?

问题描述 投票:0回答:1

他们

decimalPad
键盘没有返回按钮。所以我喜欢添加一个使用键盘工具栏的工具。但如何创建提交按钮呢?

struct ContentView: View {
    @State private var text: String = ""
    
    var body: some View {
        TextField("0", text: $text)
            .keyboardType(.decimalPad)
            .onSubmit {
                // do something
            }
        .toolbar {
            ToolbarItem(placement: .keyboard) {
                Button("Submit Button") {
                    // trigger textfield onSubmit() manually
                }
            }
        }
    }
}
swiftui onsubmit submit-button
1个回答
0
投票

你可以尝试像这样简单的事情,使用

....endEditing(true)
:

struct ContentView: View {
    @State private var text: String = ""
    
    var body: some View {
        TextField("0", text: $text).border(.red)
            .keyboardType(.decimalPad)
            .toolbar {
                ToolbarItem(placement: .keyboard) {
                    Button("Submit Button") {
                        print("do something")
                        let windowScene = UIApplication.shared.connectedScenes.first as? UIWindowScene
                        windowScene?.windows.first?.endEditing(true)
                    }
                }
            }
    }
}
© www.soinside.com 2019 - 2024. All rights reserved.