cocoa - How to Limit the Number of Tokens in a NSTokenField? -


i have nstokenfield tokens created upon hitting enter. limit number of tokens in field. example, user should allowed enter 2 tokens 1 after other. later, neither user should allowed set token nor user should allowed search further. in short, user should blocked after 2 tokens.

could 1 please me in achieving this???

thanks in advance :)

the solution divided in 2 parts:

-(nsarray *)tokenfield:(nstokenfield *)tokenfield shouldaddobjects:(nsarray *)tokens atindex:(nsuinteger)index {     //limit tokens     if(self.tokenslimit)     {         nsarray * tokensarray = [_tokenfield objectvalue];          if([tokensarray count] > 0)         {             if([tokens isequaltoarray:tokensarray])             {                 return tokens;             }             else if([tokensarray count]>=self.tokenslimit)             {                 return @[];             }             else if([tokens count]>0)             {                 tokens = [tokens subarraywithrange:nsmakerange(0, min([tokens                                                                        count], self.tokenslimit))];             }             else                 return @[];         }         else         {             tokens = [tokens subarraywithrange:nsmakerange(0, min([tokens count], self.tokenslimit))];         }     }      return tokens; } 

where tokenslimit int > 0 delegate covers cases tokens added copy/paste, completion list, drag&drop, manually written etc..

this other delegate cover case user write string , hit "tab"

- (bool)control:(nscontrol *)control isvalidobject:(id)object {     if(self.tokenslimit)     {         nsarray * tokensarray = [_tokenfield objectvalue];         tokensarray = [tokensarray subarraywithrange:nsmakerange(0, min([tokensarray count], self.tokenslimit))];         [_tokenfield setobjectvalue:tokensarray];     }     return yes; } 

Comments