8

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"?

Alex
  • 7,432
  • 20
  • 75
  • 118
  • Do you do any form of dynamic window or view replacement? – trojanfoe Jul 06 '12 at 11:41
  • @trojanfoe, I do override the drawRect: method and insert my own code in there, which draws on top of whatever UI was in the nstextfield. Is that what you mean by window/view replacement? – Alex Jul 06 '12 at 11:46
  • No I meant do you replace views within the window; the issue being you need to tell the window to recalculate the key loop if you do. I think this might be a dup of http://stackoverflow.com/questions/2217905/pressing-tab-in-nstextfield-selects-all-text-instead-of-going-to-next-control – trojanfoe Jul 06 '12 at 11:47
  • I don't believe I am replacing views in the window (no code that would suggest that), but the situation does seem to be similar to what is described in that SO link: I press TAB, the cursor becomes invisible, and pressing TAB again will select the whole text in the same nstextfield. I have tried adding recalculateKeyViewLoop in drawRect: , but nothing changes. – Alex Jul 06 '12 at 12:02
  • 2
    You could try setting the `next item` from with IB (i.e. explicitly setting the key loop rather than relying on it being calculated). Also remove that call to `recalculateKeyViewLoop`. – trojanfoe Jul 06 '12 at 12:09
  • @trojanfoe, still the same problem. tabbing just goes outside and then inside of the custom nstextfield – Alex Jul 06 '12 at 13:09
  • Hmmm, that sounds like you have some *other* control in the view which is taking first responder status, but you cannot see it (hidden, off the view, etc.). Xcode normally warns you about issues in the XIB; do you see any warnings? Also you can look through the view hierarchy in the *Objects* bit in the left-hand pane of IB. – trojanfoe Jul 06 '12 at 13:12

3 Answers3

12

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

Michael Dautermann
  • 88,797
  • 17
  • 166
  • 215
  • It's still the same problem: tabbing just goes outside (does NOT focus in another control) and then inside of the custom nstextfield. The nextKeyView does work, as the other nstextfields on the view tab in-out correctly – Alex Jul 06 '12 at 13:10
  • I wonder if setting a breakpoint on nextKeyView (or setNextKeyView) in the Xcode debugger might help? – Michael Dautermann Jul 06 '12 at 13:17
3

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.

Alex
  • 7,432
  • 20
  • 75
  • 118
  • Hey! this post is super helpful. It works for me. I have been googling for one hour or more so to be able to find this post. – Benny Khoo Dec 28 '16 at 15:45
1

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
WINSergey
  • 1,977
  • 27
  • 39