Re: Programmatically switching text fields
Re: Programmatically switching text fields
- Subject: Re: Programmatically switching text fields
- From: Ken Thomases <email@hidden>
- Date: Sun, 02 Aug 2015 21:13:32 -0500
On Aug 2, 2015, at 7:29 PM, Graham Cox <email@hidden> wrote:
> I have a series of NSTextFields and I want to automatically move the keyboard focus to the ‘next’ field when the one preceding it has a certain number of characters entered. My code is:
>
> - (void) controlTextDidChange:(NSNotification*) obj
> {
> NSTextField* control = [obj object];
>
> NSString* text = [control stringValue];
>
> if([text length] >= 4 )
> {
> NSString* shortStr = [[text substringToIndex:4] uppercaseString];
> [control setStringValue:shortStr];
>
> NSResponder* nextField = [control nextKeyView];
>
> NSLog(@"got 4 characters: '%@', moving to field: %@", shortStr, nextField );
>
> [[self window] makeFirstResponder:nextField];
> }
> }
You can try [[self window] selectKeyViewFollowingView:control] instead.
If that doesn't work, you can try [control.currentEditor tryToPerform:@selector(insertTab:) with:self].
> … the code does what it should, detecting the string length and calling -makeFirstResponder when it gets a length of 4 or more …
>
> The current field does lose focus, but the next field never gains it, so the flow of text entry from field to field doesn’t occur as it should. Also, even though the first field is set as the window’s initialFirstResponder, and does get the keyboard focus ring, the actual field editor isn’t ready and typing just produces a beep - the user has to click in the field to make it accept text. These probems may well be related.
Are you certain the window is key? Does your app do anything unusual with activationPolicy or activation, generally?
Regards,
Ken
_______________________________________________
Cocoa-dev mailing list (email@hidden)
Please do not post admin requests or moderator comments to the list.
Contact the moderators at cocoa-dev-admins(at)lists.apple.com
Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden