Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How Can I Know When NSTextView Loses Focus?

I want to clear the selection of NSTextView after it loses focus. How can I achieve that? Thanks in advance!

Kai.

like image 502
nonamelive Avatar asked Feb 05 '11 15:02

nonamelive


1 Answers

Set up a delegate object (or use your app delegate) that conforms to NSTextDelegate protocol. Then all you need to do is to implement textDidEndEditing: to clear the selection. From the docs:

textDidEndEditing:

Informs the delegate that the text object has finished editing (that it has resigned first responder status).

- (void)textDidEndEditing:(NSNotification *)aNotification

Something like:

#ifndef NSZeroRange
#define NSZeroRange NSMakeRange(0,0)
#endif

- (void)textDidEndEditing:(NSNotification *)aNotification {

    [myTextView setSelectedRange:NSZeroRange];
}
like image 120
sidyll Avatar answered Oct 03 '22 19:10

sidyll