清除 UITextField 上的按钮



我有一个带有日期选择器的UITextField,我想使用默认的内置按钮来清除文本字段,它似乎不起作用。

我尝试覆盖

func textFieldShouldClear(_ textField: UITextField) -> Bool {
        searchBar.text = "" //my textfield is called searchBar and I tried to clear the text like that
        return true
    }

似乎调用了该方法,打印文本字段说它是空的,但不是,并且在视图中显示我输入的最后日期。有什么想法吗?提前谢谢。

您正在搜索以下内容: textfield.clearButtonMode

您是否尝试过在函数中将搜索栏替换为 textField(局部变量(?

func textFieldShouldClear(_ textField: UITextField) -> Bool {
        textField.text = "" // use local variable textfield here
        return true
    }

我发现清除按钮正在更新文本字段,并且仍然从日期选择器中获取日期......现在我正在尝试清除日期选择器的值,也许这会起作用。

最新更新