0
votes

How can I get a notification when the textfield has ended editing (enter key pressed, clicked outside the text field, clicked inside the same column, but outside the text field, etc)

I checked

- (void)textDidEndEditing:(NSNotification *)aNotification
{
   //some code here
}

but this notification is only called when the text has actually changed. I need to be notified even if no text changes have been made.

Edit: Some sort of notification that the first responder has changed would work as well.

Any ideas?

1

1 Answers

0
votes

Observe the firstResponder state of the window …

…
[theWindow addObserver:self forKeyPath:@"firstResponder" options:NSKeyValueObservingOptionOld context:NULL];
…

… and read the field editor's delegate:

- (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary *)change context:(void *)context
{
  if ([keyPath isEqualToString:@"firstResponder"])
  {
    NSResponder *oldResponder =change[NSKeyValueChangeOldKey];
    if ([oldResponder isKindOfClass:[NSTextView class]])
    {
      NSTextView *editor = (NSTextView*)oldResponder;
      NSTextField *textField = (NSTextField*)editor.delegate;
      NSLog(@"This text field lost the focus: %@", textField.identifier);
    }
  }
}