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

Custom search option to the delegate #77

Closed jmig closed 10 years ago

jmig commented 10 years ago

Add the option to delegate the search to the... delegate.

I used TITokenField in various app / projects (thanks for the library by the way), and a few times I needed to have either a custom search Algorithm or simply do an API call for the search.

There are various possibility to achieve that with the current TITokenField, but it is painful and add complexity.

I simply add 2 delegate methods

- (BOOL)tokenField:(TITokenField *)field shouldUseCustomSearchForSearchString:(NSString *)searchString;
- (void)tokenField:(TITokenField *)field performCustomSearchForSearchString:(NSString *)searchString withCompletionHandler:(void (^)(NSArray *results))completionHandler;

If your delegate implement shouldUseCustomSearchForSearchString and return YES Then performCustomSearchForSearchString is called on your delegate. Your delegate is now responsible for performing the search, you can do whatever you want including an API call. All you need to do when you have your results ready is to call completionHandler(results); (ala background fetch ^^).

In the demo project I added a fun example, try searching for contributors.