Re: Programmatically switching text fields

2015-08-03 Thread Alex Zavatone
Why not store all your references in an array (a little lookup table), then set 
the firstResponder to the reference from the index of the object in the array + 
1 and roll over to index 0 if the index is past the count of the array?

Shouldn't it be [self makeFirstResponder:myTextFieldRef]?


On Aug 2, 2015, at 10:13 PM, Ken Thomases wrote:

> On Aug 2, 2015, at 7:29 PM, Graham Cox  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 (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/zav%40mac.com
> 
> This email sent to z...@mac.com


___

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

Re: Programmatically switching text fields

2015-08-03 Thread Graham Cox

> On 4 Aug 2015, at 12:17 am, Alex Zavatone  wrote:
> 
> Why not store all your references in an array (a little lookup table), then 
> set the firstResponder to the reference from the index of the object in the 
> array + 1 and roll over to index 0 if the index is past the count of the 
> array?
> 

But why? Views already have a perfectly servicable mechanism for linking a 
series of fields (or any other items) in a sequence, called the ‘nextKeyView’ 
outlet.

The problem I’m having is not determining which field to select, but making it 
actually select so that the user can type into it without needing it to be 
clicked again, despite showing the focus ring.


> Shouldn't it be [self makeFirstResponder:myTextFieldRef]?


This code is in a NSWindowController, not a NSWindow. While that is a 
NSResponder subclass and does respond to the message, it’s the window that 
selects among its subviews - at least that’s how I’ve always written code like 
this. I will check whether doing this makes any difference though.

—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