【问题标题】:UITextField crash undo keyboard iconUITextField 崩溃撤消键盘图标
【发布时间】:2020-12-22 16:17:50
【问题描述】:

在 iPad 应用程序中遇到此错误。在文本字段中将出现键盘。写任何文本,如“qwertyuio”,然后多次复制并粘贴此文本,直到达到限制。现在,从键盘应用程序按撤消将崩溃。我试图搜索但找不到任何解决方案。 例外: “由于未捕获的异常'NSRangeException'而终止应用程序,原因:'NSMutableRLEArray replaceObjectsInRange:withObject:length:: Out of bounds'”

代码,我正在使用:

func textField(_ textField: UITextField, shouldChangeCharactersIn range: NSRange, 
 replacementString string: String) -> Bool {
   if textField.text?.count >= 40 && string != ""{
                
      return false
                
   }
  return true
}

请提供解决方案。 谢谢

【问题讨论】:

    标签: ios ipad uitextfield swift4.2 undo-redo


    【解决方案1】:

    挖了几个小时后。我找到了解决方案。

       var textSize = 0
                
       if range.length == 0 {
          //adding
          textSize = range.location + string.count
       }else{
           //removing from field
           textSize = range.location - range.length
        }
                
        if textSize >= 50 {
    
           return false
    
        }else {
           if (string.isEmpty && range.length > 0) {
           textField.text = textField.text!.count > range.length ? String(textField.text!.dropLast(range.length)) : ""
           return false
        }
    

    现在一切正常。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2015-12-16
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-11-19
      相关资源
      最近更新 更多