ios - 当 UITextfield 更改时禁用键盘

标签 ios swift uitextfield

在发布此问题之前,我已经检查了所有可能的线程,但找不到好的解决方案。

我的 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/

相关文章:

html - 在 `UITextfield` 中显示来自 HTML 代码的文本

ios - 如何获取 firebase ios 可读的崩溃日志

ios - CocoaPods 规范 lint 引发错误

ios - 创建一个带有渐变边框和渐变文本的透明按钮

ios - 以编程方式创建 UItextfield,稍后如何在 Swift 中获取文本?

ios - 使用 UITextBorderStyleNone 为 UITextField 设置填充

ios - 类似终端的应用程序 : UItextFied vs UITextView

iphone - CLLocationManager 初始化并调用按钮

ios - 当应用程序重新启动到后台时,是否会调度主队列?

ios - 如何处理谷歌地图中标记簇中的单个标记