objective-cuitextviewuitextrange

Observing caret movement in Objective C


I've been trying to write a method that would observe the caret and auto-scroll to a line where caret is. I managed to scroll whenever user types in something but when it comes to observing the caret with key-value observing like this:

[_textView.selectedTextRange addObserver:_textView forKeyPath:@"start" options:NSKeyValueObservingOptionNew context:nil];

I get an error that I could not handle. I'm pretty sure that something is wrong with the above line but don't get what it is. How can I fix it or is there any other way to keep track of caret's location?


Solution

  • Apparently UITextView is not KVO compliant and that was the problem. So I used delegates to make the observation as it is suggested here