【发布时间】:2021-12-02 12:01:29
【问题描述】:
我正在处理 Swift 中的文本字段,我将要实现的是禁用导航按钮,除非文本字段中有 0 个字符,如果文本字段中的字符超过文本字段上的 1 个字母,类似于 iOS 默认日历应用。
当标题文本字段中没有字母时,栏按钮项“添加”被禁用。
启用“添加”按钮并在标题文本字段中有 1+ 个字母时显示删除按钮。
我查看了文本字段委托方法,我认为我可以通过使用 shouldChangeCharactersIn 方法来实现我正在尝试的方法 (?)
func textField(_ textField: UITextField, shouldChangeCharactersIn range: NSRange, replacementString string: String) -> Bool {
// check if there is a letter in the text field
// if there is, enable the Add button and show delete icon
// if there is not, disable the add button and hide the delete icon
return true
}
我想知道是否还有其他方法可以实现此功能?我想每当在文本字段上输入/删除一个字母时,上面的函数都会被调用,所以我想知道是否有其他方法可以轻松实现这些东西。
【问题讨论】:
-
您要检查单个文本字段还是多个文本字段是否有单个字符?
标签: ios swift uitextfield