NSTokenField delegate

1k views Asked by At

In Apple's "Token Field Programming Guide for Cocoa" in section "Basic Interaction With the Delegate" tells that when the user type some text in token field the delegate receives the tokenField:completionsForSubstring:indexOfToken:indexOfSelectedItem: message.

And when the user types the tokenizing character delegate receives the tokenField:representedObjectForEditingString: message.

So when I implemented this methods its behave themselves in a very different way.

There is my implementation of delegate methods:

- (NSArray *)tokenField:(NSTokenField *)tokenFieldArg
completionsForSubstring:(NSString *)substring 
           indexOfToken:(NSInteger)tokenIndex 
    indexOfSelectedItem:(NSInteger *)selectedIndex {

NSLog(@"completionsForSubstring: %@", substring);
return [NSArray array];
}    

- (id)tokenField:(NSTokenField *)tokenField
 representedObjectForEditingString:(NSString *)editingString {

NSLog(@"representedObjectForEditingString: %@", editingString);
return editingString;
}

And there is a log when i type "maksim" in token field and then erase it:

2011-03-01 19:03:01.286 Aurum[23269:a0f] representedObjectForEditingString: m
2011-03-01 19:03:01.287 Aurum[23269:a0f] representedObjectForEditingString: m
2011-03-01 19:03:01.493 Aurum[23269:a0f] completionsForSubstring: m
2011-03-01 19:03:01.633 Aurum[23269:a0f] representedObjectForEditingString: ma
2011-03-01 19:03:01.633 Aurum[23269:a0f] representedObjectForEditingString: ma
2011-03-01 19:03:01.637 Aurum[23269:a0f] completionsForSubstring: ma
2011-03-01 19:03:03.737 Aurum[23269:a0f] representedObjectForEditingString: mak
2011-03-01 19:03:03.737 Aurum[23269:a0f] representedObjectForEditingString: mak
2011-03-01 19:03:03.741 Aurum[23269:a0f] completionsForSubstring: mak
2011-03-01 19:03:05.089 Aurum[23269:a0f] representedObjectForEditingString: maks
2011-03-01 19:03:05.089 Aurum[23269:a0f] representedObjectForEditingString: maks
2011-03-01 19:03:05.094 Aurum[23269:a0f] completionsForSubstring: maks
2011-03-01 19:03:05.841 Aurum[23269:a0f] representedObjectForEditingString: maksi
2011-03-01 19:03:05.841 Aurum[23269:a0f] representedObjectForEditingString: maksi
2011-03-01 19:03:05.845 Aurum[23269:a0f] completionsForSubstring: maksi
2011-03-01 19:03:06.697 Aurum[23269:a0f] representedObjectForEditingString: maksim
2011-03-01 19:03:06.697 Aurum[23269:a0f] representedObjectForEditingString: maksim
2011-03-01 19:03:06.701 Aurum[23269:a0f] completionsForSubstring: maksim
2011-03-01 19:03:12.353 Aurum[23269:a0f] representedObjectForEditingString: maksi
2011-03-01 19:03:12.354 Aurum[23269:a0f] representedObjectForEditingString: maksi
2011-03-01 19:03:12.853 Aurum[23269:a0f] representedObjectForEditingString: maks
2011-03-01 19:03:12.854 Aurum[23269:a0f] representedObjectForEditingString: maks
2011-03-01 19:03:12.936 Aurum[23269:a0f] representedObjectForEditingString: mak
2011-03-01 19:03:12.937 Aurum[23269:a0f] representedObjectForEditingString: mak
2011-03-01 19:03:13.020 Aurum[23269:a0f] representedObjectForEditingString: ma
2011-03-01 19:03:13.020 Aurum[23269:a0f] representedObjectForEditingString: ma
2011-03-01 19:03:13.103 Aurum[23269:a0f] representedObjectForEditingString: m
2011-03-01 19:03:13.104 Aurum[23269:a0f] representedObjectForEditingString: m

So can anyone explain me what's going wrong and why token field behave themself so strange?

1

There are 1 answers

1
Stephen Poletto On

I just tried to reproduce this by copying and pasting your code. I got:

2011-03-09 14:52:48.888 StackOverflowTesting[27789:a0f] completionsForSubstring: m
2011-03-09 14:52:49.242 StackOverflowTesting[27789:a0f] completionsForSubstring: ma
2011-03-09 14:52:49.560 StackOverflowTesting[27789:a0f] completionsForSubstring: mak
2011-03-09 14:52:49.911 StackOverflowTesting[27789:a0f] completionsForSubstring: maks
2011-03-09 14:52:50.017 StackOverflowTesting[27789:a0f] completionsForSubstring: maksi
2011-03-09 14:52:50.217 StackOverflowTesting[27789:a0f] completionsForSubstring: maksim
2011-03-09 14:52:50.647 StackOverflowTesting[27789:a0f] representedObjectForEditingString: maksim

Which is exactly what'd I'd expect as the output. Have you changed the default tokenizing characters of the NSTokenField? When you hit the tokenizing character, it doesn't look like it tokenized maksim (since after all, you were able to delete it character by character).