在发布此问题之前,我已经检查了所有可能的线程,但找不到好的解决方案。
我的 View 中有 5 个文本字段,选择前 2 个文本字段时应出现键盘,选择后 3 个文本字段时应出现 PickerView。一切工作正常,因为我使用标签来识别我的文本字段,并且在“textFieldShouldBeginEditing”中我为最后 3 个文本字段返回了 False。
但是,如果我选择文本字段 2,然后移至文本字段 3,键盘将出现在选择器 View 上方。 我尝试过使用 textFieldDidEndEditing 但它不起作用。
还有其他方法可以解决这个问题吗?
我在每个文本字段上使用 addTarget 来调用此函数
func openDatesPicker() {
ActionSheetStringPicker.show(withTitle: "Appointment Date", rows: datesArray , initialSelection: selectedDateIndex , doneBlock: {
picker, value, index in
self.selectedDateIndex = value
if let selected = index as? String{
self.dateTF.text = selected
let timeIndex = self.findEarliestTime(date: self.dateTF.text!)
self.timeTF.text = timeIndex
}
return
}, cancel: { ActionStringCancelBlock in return }, origin: self.view)
}
最佳答案
请将此语句添加到 openDatesPicker 方法的开头
self.view.endEditing(true)
根据文档:
This method looks at the current view and its subview hierarchy for the text field that is currently the first responder. If it finds one, it asks that text field to resign as first responder. If the force parameter is set to true, the text field is never even asked; it is forced to resign.
关于ios - 当 UITextfield 更改时禁用键盘,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45766804/