【发布时间】:2020-02-09 11:52:58
【问题描述】:
我在 SwiftUI 中有一个 TextField,它需要使用不同的键盘,具体取决于由 SegementedControl() 选择器确定的 @State 变量的值。
当用户点击不同的片段时,如何关闭键盘(如发送 endEditing 事件)?我需要这样做,因为我想更改键盘类型,如果 textField 是响应者,则键盘不会更改。
我有这个扩展:
extension UIApplication {
func endEditing() {
sendAction(#selector(UIResponder.resignFirstResponder), to: nil, from: nil, for: nil)
}
}
我可以做类似的事情
UIApplication.shared.endEditing()
但是当用户点击不同的段时,我不知道在哪里或如何调用它。
我尝试在选择器上放置一个 tapGesture 并且键盘确实关闭了,但点击没有传递到选择器,因此它不会改变。
代码 sn-p 在这里:
@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)
}
}
【问题讨论】:
标签: ios swiftui resignfirstresponder