在SwiftUI中,我有一个TextField需要根据由SegmentedControl()选择器确定的@State变量的值使用不同的键盘。
当用户点击不同的段时,如何关闭键盘(例如发送endEditing事件)?我需要这样做是因为我想要更改键盘类型,如果textField是响应者,则键盘不会更改。
我有这个扩展:
extension UIApplication {
func endEditing() {
sendAction(#selector(UIResponder.resignFirstResponder), to: nil, from: nil, for: nil)
}
}
我可以做类似这样的事情
UIApplication.shared.endEditing()
但我不知道在用户点击不同的选项时应该在哪里或如何调用它。
我尝试在Picker上放置一个tapGesture,键盘确实消失了,但是点击事件没有传递到Picker,因此它没有改变。
代码片段如下:
@State private var type:String = "name"
请提供需要翻译的内容。Form {
Section(header: Text("Search Type")) {
Picker("", selection: $type) {
Text("By Name").tag("name")
Text("By AppId").tag("id")
}.pickerStyle(SegmentedPickerStyle())
}
Section(header: Text("Enter search value")) {
TextField(self.searchPlaceHolder, text: $searchValue)
.keyboardType(self.type == "name" ? UIKeyboardType.alphabet : UIKeyboardType.numberPad)
}
}