TAB in custom NSTextField does not put focus on another control
Asked Answered
M

3

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

Marissamarist answered 6/7, 2012 at 11:38 Comment(7)
Do you do any form of dynamic window or view replacement?Connatural
@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?Marissamarist
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 #2218405Connatural
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.Marissamarist
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.Connatural
@trojanfoe, still the same problem. tabbing just goes outside and then inside of the custom nstextfieldMarissamarist
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.Connatural
M
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.

Marissamarist answered 6/7, 2012 at 13:56 Comment(1)
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.Renny
A
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

Assyria answered 6/7, 2012 at 12:48 Comment(2)
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 correctlyMarissamarist
I wonder if setting a breakpoint on nextKeyView (or setNextKeyView) in the Xcode debugger might help?Assyria
M
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.

Marissamarist answered 6/7, 2012 at 13:56 Comment(1)
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.Renny
P
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 https://mcmap.net/q/1167362/-prevent-selecting-all-tokens-in-nstokenfield
        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
Pollard answered 21/5, 2014 at 21:43 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.