I am having difficulties with setting a tab-order for my NSTextField
s.
In my AppDelegate
I add a NSViewController
- (void)applicationDidFinishLaunching:(NSNotification *)aNotification {
CustomViewController *vc = [[CustomViewController alloc] init];
[_window.contentView addSubview:vc.view];
[_window setAutorecalculatesKeyViewLoop:NO];
[_window.contentView addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"H:|[view]|" options:0 metrics:nil views:@{@"view":vc.view}]];
[_window.contentView addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"V:|[view]|" options:0 metrics:nil views:@{@"view":vc.view}]];
}
Then in my NSViewController
I add custom NSViews
which contain a label and a text field.
- (void)viewDidLoad {
_customView = [[CustomView alloc] initWithLabel:@"Foo"];
[self.view addSubview_customView];
_customView1 = [[CustomView alloc] initWithLabel:@"Bar"];
[self.view addSubview_customView1];
_customView2 = [[CustomView alloc] initWithLabel:@"FooBar"];
[self.view addSubview_customView2];
}
And finally I have the CustomView which implements the label and text field as follows:
- (void)initWithLabel:(NSString *)label {
self = [super initWithFrame:NSZeroRect];
if (self) {
_label = [[NSTextField alloc] initWithFrame:NSZeroRect];
_label.stringValue = label;
_label.font = [NSFont fontWithName:@"HelveticaNeue-Light" size:12.0f];
_label.alignment = NSLeftTextAlignment;
_label.textColor = [NSColor grayColor];
_label.selectable = NO;
_label.editable = NO;
_label.drawsBackground = NO;
_label.bezeled = NO;
[self addSubview:_label];
_textField = [[NSTextField alloc] initWithFrame:NSZeroRect];
_textField.stringValue = @"";
_textField.alignment = NSRightTextAlignment;
_textField.font = [NSFont fontWithName:@"HelveticaNeue-Light" size:32.0f];
[self addSubview:_textField];
}
return self;
}
I do the positioning with NSLayoutConstraints
and everything looks fine and works as expected, except when I try to implement setNextKeyView:
.
I have tried to do it by using the exposed textField
in the viewDidLoad
of the view controller like:
...
[_customView.textField setNextKeyView:_customView1.textField];
[_customView1.textField setNextKeyView:_customView3.textField];
[_customView2.textField setNextKeyView:_customView.textField];
...
But that did not work. When pressing tab-key from a NSTextField
the current field loses focus, but the next one does not gain it.
I also tried calling [[[self view] window] recalculateKeyViewLoop]
afterwards but that didn't help either.
How do I go about doing this?
I also played around with setting NSWindow
setAutorecalculatesKeyViewLoop:
to YES
but that also did not haven an effect.
Thanks
PS: this is pseudo-code to simplify things. If a brace is missing or there is a typo, then that is not my problem. It compiles fine and works too. Just the tabbing is not behaving as expected. ;-)