NSTokenField set maximal number of Token

251 views Asked by At

Let's say, I have a TokenField, where the user can input who the message should be sent to. But I don't want that the user can input more than 3 Token.

Is there a way to achieve this?

1

There are 1 answers

1
Parag Bafna On BEST ANSWER

Implement NSTokenField Delegate tokenField:shouldAddObjects:atIndex:

// return an array of represented objects you want to add.
// If you want to reject the add, return an empty array.

- (NSArray *)tokenField:(NSTokenField *)tokenField shouldAddObjects:(NSArray *)tokens atIndex:(NSUInteger)index
{
    if (index>2) {
        return [NSArray array];
    }
    NSLog(@"%@-- %d %d", tokens, [tokens count],index);
    return tokens;
}