thermogl / TITokenField

An iOS version of the NSTokenField (See To: field in Mail and Messages).
http://thermoglobalnuclearwar.com/opensource/
599 stars 172 forks source link

tokenField:willAddToken: get called at strange moment #69

Open chiahsien opened 10 years ago

chiahsien commented 10 years ago

I'm trying to prevent user from adding duplicated token, and trying to limit maximum token counts.

So I try to do some checking like this:

- (BOOL)tokenField:(TITokenField *)tokenField willAddToken:(TIToken *)token {
  NSArray *titles = tokenField.tokenTitles;
  for (NSString *title in titles) {
    if ([title isEqualToString:token.title]) {
      // Prompt: duplicate!
      return NO;
    }
  }
  return (titles.count < kICDMaxNumberOfTags);
}

I'll prompt user that he is trying to add duplicated token.

But I found that this method gets called while tokenField begins editing and ends editing, which makes the prompt shows up if tokenField isn't empty.

I'm curious why addToken gets called when tokenField begins/ends editing. Any idea?

thermogl commented 10 years ago

The tokens get removed when the field loses focus, and re-adds them when the fields gets focus.

nsolter commented 9 years ago

I also ran into this. The problem is that addToken adds the token both to the model and the view. It's already in the model (_tokens) so just needs to be added as a subview. To fix it, I factored out an addTokenFromView from addToken, and called that from didBeginEditing(). I can submit this as a pull request if there's interest.