Search code examples
macosnstextfieldfirst-responder

get current first responder from NSTextField elements


Im developing an app for MacOSX in Xcode 5

I have a group of NSTextField which share the focus by pressing TAB or Enter key, when I go to last element I send the focus to my NSWindow by pressing tab as well, the thing is... I want to go to first NSTextField by pressing tab again but I can't, this is my code

-(BOOL)control:(NSControl *)control textView:(NSTextView *)textView doCommandBySelector:(SEL)commandSelector{

    NSTextField *textField = (NSTextField *) control;

    if ((commandSelector == @selector(insertTab:) || commandSelector == @selector(insertNewline:)) && (textField == self.descripcionBitacoraTextField)) {
        [self.window makeFirstResponder:[[textView window]nextResponder]];

        return YES;
    }
    else if(commandSelector == @selector(insertTab:) || commandSelector == @selector(insertNewline:)){
        [self.window makeFirstResponder:[self.contentView viewWithTag:(textField.tag +1)]];

        return YES;
    }

    return NO;
}

in this case my last NSTextField is descripcionBitacoraTextField, when I get there and press Tab or Enter again I lost the focus from all the controls, but I want to activate it by pressing Tab Again, I wanted to track which control has FirstResponder control but I don't know how

any help I'll appreciate


Solution

  • just replace:

    -(BOOL)control:(NSControl *)control textView:(NSTextView *)textView doCommandBySelector:(SEL)commandSelector{
    
        NSTextField *textField = (NSTextField *) control;
    
        if ((commandSelector == @selector(insertTab:) || commandSelector == @selector(insertNewline:)) && (textField == self.descripcionBitacoraTextField)) {
            [self.window makeFirstResponder:nil];
    
            return YES;
        }
        else if(commandSelector == @selector(insertTab:) || commandSelector == @selector(insertNewline:)){
            [self.window makeFirstResponder:[self.contentView viewWithTag:(textField.tag +1)]];
    
            return YES;
        }
    
        return NO;
    }
    

    I'm just setting

    [self.window makeFirstResponder:nil]; 
    

    instead of your

    [self.window makeFirstResponder:[[textView window]nextResponder]];