NSTextField syntax for sending textDidEndEditing

I have a subclass of NSTextField, which I made so that when the user has finished editing the field, the text field will lose focus. I also set it up so when the user clicks on the main view, this will act as a loss of focus on the text field. And all this works great. Now I want to add some additional features to the subclass.

I want the text field to send textDidEndEditing every time the user clicks anywhere outside the field. This includes when the user clicks on another component of the user interface. The behavior that I see now is that when a user clicks on another component of the user interface (say, a combo box), the action does not start. Is there any way to force this? Also, manually adding it as part of the actions of other components?

Any help would be appreciated!

Here is the code for the textDidEndEditing function

- (void)textDidEndEditing:(NSNotification *)notification
{
  NSString *file = nil;
  char c = ' ';
  int index = 0;

  [super textDidEndEditing:notification];

  if ([self isEditable])
  {
    // is there a valid string to display?
    file = [self stringValue];
    if ([file length] > 0)
    {
      c = [file characterAtIndex:([file length] - 1)];
      if (c == '\n') // check for white space at the end
      {
        // whitespace at the end... remove
        NSMutableString *newfile = [[NSMutableString alloc] init];
        c = [file characterAtIndex:index++];
        do
        {
          [newfile appendFormat:@"%c", c];
          c = [file characterAtIndex:index++];
        }
        while ((c != '\n') && (index < [file length]));

        [self setStringValue:newfile];
        file = newfile;
      }

      [[NSNotificationCenter defaultCenter]
       postNotificationName:@"inputFileEntered" object:self];
    }
  }

  // since we're leaving this box, show no text in this box as selected.
  // and deselect this box as the first responder
  [self setSelectedText:0];
  [[NSNotificationCenter defaultCenter]
   postNotificationName:@"setResponderToNil" object:self];
}

Where "setSelectedText" is a public function in a subclass of the text field:

- (void)setSelectedText:(int) length
{
  int start = 0;
  NSText *editor = [self.window fieldEditor:YES forObject:self];
  NSRange range = {start, length};
  [editor setSelectedRange:range];
}

And the setResponderToNil notification is part of my NSView subclass:

- (void)setResponderToNil
{
  AppDelegate *delegate = (AppDelegate *)[NSApp delegate];
  [delegate.window makeFirstResponder:nil];
}
+4
1

, . , , , , .

:

event_monitor_mousedown_ = [NSEvent addLocalMonitorForEventsMatchingMask:NSRightMouseDown
                                                               handler:^NSEvent *(NSEvent * event)
{
  NSResponder *resp = [[[NSApplication sharedApplication] keyWindow] firstResponder];

    if ([resp isKindOfClass:[NSTextView class]])
    {
      // set UI in proper state - remove focus from text field
      // even when touching a new window for the first time
      [[NSNotificationCenter defaultCenter]
       postNotificationName:@"setResponderToNil" object:self];
      [self setStopState];
    }

  return event;
}];

mouseDown. textView ( , NSTextField), , firstResponder nil. textDidEndEditing. , , . , - !

0

Source: https://habr.com/ru/post/1535428/


All Articles