質問

How do I prevent duplicate select from an Array in an NSTokenField. I've implemented the delegate completionsForSubstring.

役に立ちましたか?

解決 2

The best method I would say is to implement the delegate shouldAddObjects. Write the following code in the delegate.

NSString *newVal = [[tokens objectAtIndex:0] description];
NSArray *aray = [tokenField objectValue];
for (NSString * token in aray) {
    @try{
        if ([token isEqual:newVal]){
            return nil;
        }
    }
    @catch (NSException *exception) {
        ;
    }
}

他のヒント

I was able to remove any duplicates with this approach, which admittedly is a little hacky but it works:

// Be sure to set the delegate of your NSTokenfield either in IB or in code.


#pragma mark - NSTokenFieldDelegate
-(NSArray *)tokenField:(NSTokenField *)tokenField shouldAddObjects:(NSArray *)tokens atIndex:(NSUInteger)index{

    double delayInSeconds = .1;
    dispatch_time_t popTime = dispatch_time(DISPATCH_TIME_NOW, (int64_t)(delayInSeconds * NSEC_PER_SEC));
    dispatch_after(popTime, dispatch_get_main_queue(), ^(void){
        NSArray *aray = [tokenField objectValue];
        NSOrderedSet *set = [NSOrderedSet orderedSetWithArray:aray];
        aray = [set array];
        [tokenField setObjectValue:aray];

    });

    return tokens;
}
ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top