I think this came up recently in a slightly different context, but reading that 
thread doesn’t help me with my problem here.

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];
        }
}



This code is in a NSWindowController subclass, and I have set up the 
‘nextKeyView’ of each fireld to be the next one along, and I’ve also verified 
that these are all set correctly, as is the ‘window’ outlet, so the line        
     

[[self window] makeFirstResponder:nextField];


is called, and does have all valid parameters.

The window controller is set as the delegate of the fields so that it receives 
the -controlTextDidChange message as the user types in the fields. This is 
definitely called on every keystroke, and the code does what it should, 
detecting the string length and calling -makeFirstResponder when it gets a 
length of 4 or more (as a side effect it also forces whatever the user has 
types to be uppercase but that’s not important here).

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.

I should point out that this did work in the past for some version of the OS, 
but it doesn’t work in 10.10 - I don’t know at what version it stopped working.

I’ve tried calling -makeFirstResponder after a delay, but it doesn’t help. I’m 
using a custom field editor (NSTextView subclass), but if I disable that so it 
uses the standard one, the faulty behaviour doesn’t change.

What’s the currently supported way to programmatically switch among text 
fields? Or is it simply broken in the OS at the moment?

—Graham



_______________________________________________

Cocoa-dev mailing list (Cocoa-dev@lists.apple.com)

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:
https://lists.apple.com/mailman/options/cocoa-dev/archive%40mail-archive.com

This email sent to arch...@mail-archive.com

Reply via email to