Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

TAB in custom NSTextField does not put focus on another control

I have a custom NSTextField, where I'm implementing some rounded corners.

Pressing the "TAB" key does not go to the next NSTextField (or selectable control) in the window. Weird. Why would it do that? Is there something special I need to add to enable the app to go through the other controls when pressing "TAB"?

like image 562
Alex Avatar asked Jul 06 '12 11:07

Alex


3 Answers

Hopefully you've set the nextKeyView either programatically or in Xcode's interface builder, like so:

set nextKeyView from one text field to the next

like image 191
Michael Dautermann Avatar answered Sep 19 '22 20:09

Michael Dautermann


Seems like it was my fault.

I was incorporating delegate calls within the custom class for textDidBeginEditing: and textDidEndEditing:, in order to maintain the placeholder text when the user tabs out of the field, but I wasn't calling the respective super class' methods as well.

After including the call to [super textDidEndEditing...] and [super textDidBeginEditing...] tabbing works fine.

like image 30
Alex Avatar answered Sep 20 '22 20:09

Alex


My solution is not a great one, but works:

Subclass NSTextView

#import <Cocoa/Cocoa.h>

@interface NMTextView : NSTextView

@end


#import "NMTextView.h"

@implementation NMTextView

#pragma clang diagnostic push
#pragma clang diagnostic ignored "-Warc-performSelector-leaks"

- (void)keyDown:(NSEvent *)theEvent{

    switch ([theEvent keyCode]) {
        case 36:{
            if (([theEvent modifierFlags] & NSCommandKeyMask))
                //something for Ctrl+Enter
            else
                [super insertNewlineIgnoringFieldEditor:self];
        }break;

        case 48:
            //[self nextKeyView] = _NSClipViewOverhangView
            //[[self nextKeyView] nextKeyView] = NSTokenField (in my case)
            // or something different
            [[[self nextKeyView] nextKeyView] becomeFirstResponder];
            //also http://stackoverflow.com/a/3008622/1067147
        break;

        case 53:
            [_target performSelector:_actionEsc withObject:self];
        break;

        default:// allow NSTextView to handle everything else
            [super keyDown:theEvent];
        break;
    }
}

#pragma clang diagnostic pop

@end
like image 26
WINSergey Avatar answered Sep 20 '22 20:09

WINSergey