NSTokenField does not check blur marker

I have an NSTokenField that allows the user to select contacts (exactly the same as in Mail.app). So NSTextField is bound to an array in my instance variable model.recipient .

Now the user can select an entry from the list of automatic completion, for example. Joe Bloggs: joe@blogs.com and as soon as he presses Enter , a token (Joe Bloggs) model.recipients and model.recipients now contains a BBContact entry.

Now, if the user starts to enter some keys (suggestions are displayed this way), and then displays Tab instead of entering a marker with the completion text value (Joe Bloggs: joe@bloggs.com ) , and the NSTokenFieldDelegate methods NSTokenFieldDelegate called so that I can respond to this event. The model.recipient now contains an NSString instead of the BBContact .

Curiously, the tokenField:shouldAddObjects:atIndex: delegate method is not called, which I expect when the user selects a token from the field.

enter image description here

+7
source share
3 answers

Clicking the tabs causes isValidObject in the delegate, so return NO for NSTokenField, but you want to return YES if it does not contain alphanumeric characters, otherwise the user will not be able to focus away from the field (the string contains invisible Unicode characters based on how many tokens exist )

A less fragile implementation that I could come up with is:

 - (BOOL)control:(NSControl *)control isValidObject:(id)token { if ([control isKindOfClass:[NSTokenField class]] && [token isKindOfClass:[NSString class]]) { if ([token rangeOfCharacterFromSet:[NSCharacterSet alphanumericCharacterSet]].location == NSNotFound) return YES; return NO; } return YES; } 
+6
source

I was able to solve the problem using @valexa's suggestions. In case of blurring with TAB I need to go through all the entries and find my contact objects for any lines.

 - (BOOL)control:(NSControl *)control isValidObject:(id)token{ if ([control isKindOfClass:[NSTokenField class]] && [token isKindOfClass:[NSString class]]) { NSTokenField *tf = (NSTokenField *)control; if ([token rangeOfCharacterFromSet:[NSCharacterSet alphanumericCharacterSet]].location == NSNotFound){ return YES; } else { // We get here if the user Tabs away with an entry "pre-selected" NSMutableArray *set = @[].mutableCopy; for(NSObject *entry in tf.objectValue){ GSContact *c; if([entry isKindOfClass:GSContact.class]){ c = (GSContact *)entry; } if([entry isKindOfClass:NSString.class]){ NSString *number = [[(NSString *)entry stringByReplacingOccurrencesOfString:@">" withString:@""] componentsSeparatedByString:@"<"][1]; c = [self findContactByNumber:number]; } if(c) [set addObject:c]; } [control setObjectValue:set]; } } return YES; } 

enter image description here

0
source

This may be due to the fact that the enter key can send a marker field event to the action, where the tab key simply adds text to it. You can try setting the -isContinuous property to YES and see if you get the desired results.

-one
source

All Articles