UITextView委托多次调用 [英] UITextView delegate calling multiple times

查看:106
本文介绍了UITextView委托多次调用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用UITextView并实现了委托函数

I am using UITextView and implemented the delegate function

var count = 0
func textView(_ textView: UITextView, shouldChangeTextIn range: NSRange, replacementText text: String) -> Bool {
    print(text)
    count += 1
    print(count)
    return true
}

样本

当我从键盘"中选择联想文字时,应当"ChangedTextInRange"委托调用两次.

When i select the predictive text from the Keyboard, shouldChangeTextInRange delegate is calling twice.

  1. 为什么这个代表打了两次电话?
  2. 为什么这种情况仅发生在预测文本上

推荐答案

请使用此代码.它将运行良好,并希望它将与您现有的逻辑完美配合.

Please use this code. It will work fine and hope It will be perfectly working with your existing logic.

 var count = 0
    func textView(_ textView: UITextView, shouldChangeTextIn range: NSRange, replacementText text: String) -> Bool {
        print(text)
        let trimmedString = text.trimmingCharacters(in: .whitespaces)
        if(trimmedString.characters.count != 0){
        count += 1
        print(count)
        }
        return true
    }

问题1和2的答案是 当您从联想文字中选择文字时.首先,它附加一个单词,然后附加一个空格.这就是委托被两次调用的原因.

Answer for both question 1 and 2 is When you select text from the predictive text. First, it appends word then It appends a space. That's the reason delegate is called twice.

这篇关于UITextView委托多次调用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆