答案 0 :(得分:1)
尝试使用shouldChangeTextInRange
代替textViewDidChange
我尝试了它,它是用预测文本触发的
答案 1 :(得分:0)
您是否设置了textView.delegate?
答案 2 :(得分:0)
YourViewController<UITextViewDelegate>
textView.delegate = self
func textViewDidChange(textView: UITextView)
:
func textViewDidChange(textView: UITextView) {
//textView(Sender)
if(textView == yourtextview) {
//do something
}
}
答案 3 :(得分:0)
当UITextView更改时,我必须使用NotificationCenter来获得通知。
我创建了UITextView的扩展以注册/注销这些通知。请记住,当您不再希望继续处理这些更改时(例如,在viewWillDisappear
上,请致电取消注册。
import UIKit
extension UITextView {
public func registerTextViewNotifications() {
let center = NotificationCenter.default
center.addObserver(self,
selector: #selector(textViewDidChangeWithNotification(_:)),
name: UITextView.textDidChangeNotification,
object: nil)
}
public func unregisterTextViewNotifications() {
let center = NotificationCenter.default
center.removeObserver(self,
name: UITextView.textDidChangeNotification,
object: nil)
}
@objc private func textViewDidChangeWithNotification(_ notification: Notification) {
// Do something when edited
print("Text: \(String(describing: text))")
}
}
class ViewController: UIViewController {
@IBOutlet var textView: UITextView!
override func viewDidLoad() {
super.viewDidLoad()
}
override func viewWillAppear(_ animated: Bool) {
super.viewWillAppear(animated)
textView.registerTextViewNotifications()
}
override func viewWillDisappear(_ animated: Bool) {
super.viewWillDisappear(animated)
textView.unregisterTextViewNotifications()
}
}