Search code examples
macossubclassnstextfieldnsresponder

Undo makeFirstResponder


I want an NSTextField that prevents empty strings so I subclassed the NSTextField and implemented this method

-(void) textDidEndEditing:(NSNotification *)aNotification
{
  if([[self stringValue] isEqualToString:@""])
  {
    NSBeep();
   [[self window] makeFirstResponder:self];
  }
  else
  {
    //what goes here
  }
}

This works when my new text field is the second control in the window but not the first. In those cases I can't tab out of the Subclassed textfield even when its text is non-empty

So, how do I undo the makeFirstResponder method? Or is there a way of making the new textfield the current responder

Thanks in advance

stupot.


Solution

  • As you're overriding the base function, you should only need to make a call back to the super to make normal behaviour continue.

    -(void) textDidEndEditing:(NSNotification *)aNotification
    {
      if([[self stringValue] isEqualToString:@""])
      {
        NSBeep();
       [[self window] makeFirstResponder:self];
      }
      else
      {
        //what goes here - this:
        [super textDidEndEditing:aNotification];
      }
    }