I want to observe the var text! property of UITextView, I do not want to use a delegate.
My solution is this:
class MessageTextView: UITextView {
override var text: String! {
didSet {
print(text)
}
}
}
But it does not get called when typing in the textfield of the UITextView. I tried textStorage and selectedRange as well. For me it only makes sense to override text, but it does not work. Any solution?
Edit: This is my solution:
deinit {
NotificationCenter.default.removeObserver(self, name: UITextView.textDidChangeNotification, object: nil)
}
func setObserverNotification() {
NotificationCenter.default.addObserver(self, selector: #selector(updateText), name: UITextView.textDidChangeNotification, object: nil)
}
@objc private func updateText() {
// Do updates here
}