TAB in custom NSTextField does not put focus on another control
-
19-06-2021 - |
문제
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"?
해결책 2
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.
다른 팁
Hopefully you've set the nextKeyView either programatically or in Xcode's interface builder, like so:
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