ios - UITextContentType* 不工作

我在 UITextfield 子类上使用 UITextContentType GivenName/FamilyName 以允许用户在注册表单中快速填写个人详细信息。

出于某种原因,当用户点击键盘上方建议栏中的建议名称时,它不会填满 textfield。相反,它正在填充单个空间。

我已经与 textfield 代表核对过,

-(BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string

我在新测试中也得到了一个空格。

有人遇到同样的问题并且知道这个问题的解决方案吗?

最佳答案

委托(delegate)方法可能是导致问题的原因

func textField(_ textField: UITextField, shouldChangeCharactersIn range: NSRange, replacementString string: String) -> Bool {
    return false
}

这是一个奇怪的行为 委托(delegate)方法将首先用空字符串调用一次。然后,如果该方法返回 false,则不会发生任何其他事情。

但是如果你返回 true 它会用正确的替换字符串再次调用

引用资料 https://stackoverflow.com/a/55721707/1463518

解决办法

func textField(_ textField: UITextField, shouldChangeCharactersIn range: NSRange, replacementString string: String) -> Bool {
    if string.isEmpty || string == " " {
       defer {
         if let text = textField.text, let textRange = Range(range, in: text) {
       textField.text = text.replacingCharacters(in: textRange, with: "")
         }
       }
       return true
    }
    // do your stuff
    return false
}

https://stackoverflow.com/questions/49191121/

相关文章:

amazon-web-services - AWS Batch 可以运行 Windows Docke

asp.net-core - ASP.NET 核心 : Initiating a request t

python - 模块 'site' 没有属性 'getusersitepackages'

php - 错误:[8] PDO::__construct():发送 5 个字节失败,errno=3

hibernate - QueryDSL - 仅比较 dateTime 中的日期

azure-ad-b2c - 休息 API 超时

python-sphinx - 如何在 python-sphinx 中突出显示文本?

r - tcltk 对话框出现在 RStudio/Shiny Windows 下

php - 在 PHP 中从文件流创建 Zip

kubernetes - 是否可以在同一端口上使用 nodeport 类型的 2 个不同 names