gpt4 book ai didi

ios - 非 View Controller 类中的 UITextViewDelegate

转载 作者:行者123 更新时间:2023-11-30 13:59:42 25 4
gpt4 key购买 nike

我正在尝试创建一个类,其唯一目的是实现 UITextViewDelegate,但由于某种原因,从未调用方法 shouldChangeTextInRange,这是我的代码:

public class TTextViewDelegate: NSObject, UITextViewDelegate {

public func textView(textView: UITextView, shouldChangeTextInRange range: NSRange, replacementText text: String) -> Bool {

print("!!!!!! Should change text in range: \(text)")

return true
}

}

在我的 View Controller 中:

textView.delegate = TTextViewDelegate()

为什么 UITextViewDelegate 仅在 View Controller 中实现时才起作用?

最佳答案

public func textView(textView: UITextView, shouldChangeTextInRange range: NSRange, replacementText text: String) -> Bool {

每当用户键入新字符或删除现有字符时, TextView 都会调用此方法。该方法的实现是可选的。您可以使用此方法在将文本提交到 TextView 存储之前替换文本。例如,拼写检查器可能会使用此方法将拼写错误的单词替换为正确的拼写。

范围

当前选择范围。如果范围的长度为 0,则范围反射(reflect)当前插入点。如果用户按删除键,则范围的长度为 1,并且空字符串对象将替换该单个字符。

文本

要插入的文本。

关于ios - 非 View Controller 类中的 UITextViewDelegate,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33143472/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com