Permalink
Browse files

Defer token addition on 'return' to the delegate.

Also makes the delegate responsible for any pre-processing,
and removes the assumption that 'representedObject' is a string.
  • Loading branch information...
1 parent b330cfb commit bbfcae43ddaa9dfe270b1cada0bbf33e7ca54976 @bjhomer bjhomer committed Oct 17, 2011
Showing with 42 additions and 22 deletions.
  1. +23 −0 JSTokenField/DemoViewController.m
  2. +5 −0 JSTokenField/JSTokenField.h
  3. +14 −22 JSTokenField/JSTokenField.m
@@ -125,6 +125,29 @@ - (void)tokenField:(JSTokenField *)tokenField didRemoveTokenAtIndex:(NSUInteger)
NSLog(@"Deleted token %d\n%@", index, _toRecipients);
}
+- (BOOL)tokenFieldShouldReturn:(JSTokenField *)tokenField {
+ NSMutableString *recipient = [NSMutableString string];
+
+ NSMutableCharacterSet *charSet = [[[NSCharacterSet whitespaceCharacterSet] mutableCopy] autorelease];
+ [charSet formUnionWithCharacterSet:[NSCharacterSet punctuationCharacterSet]];
+
+ NSString *rawStr = [[tokenField textField] text];
+ for (int i = 0; i < [rawStr length]; i++)
+ {
+ if (![charSet characterIsMember:[rawStr characterAtIndex:i]])
+ {
+ [recipient appendFormat:@"%@",[NSString stringWithFormat:@"%c", [rawStr characterAtIndex:i]]];
+ }
+ }
+
+ if ([rawStr length])
+ {
+ [tokenField addTokenWithTitle:rawStr representedObject:recipient];
+ }
+
+ return NO;
+}
+
- (void)handleTokenFieldFrameDidChange:(NSNotification *)note
{
if ([[note object] isEqual:_toField])
@@ -61,7 +61,12 @@ extern NSString *const JSDeletedTokenKey;
@protocol JSTokenFieldDelegate <NSObject>
+@optional
+
- (void)tokenField:(JSTokenField *)tokenField didAddToken:(NSString *)title representedObject:(id)obj;
- (void)tokenField:(JSTokenField *)tokenField didRemoveTokenAtIndex:(NSUInteger)index;
+- (BOOL)tokenFieldShouldReturn:(JSTokenField *)tokenField;
+- (void)tokenFieldDidEndEditing:(JSTokenField *)tokenField;
+
@end
@@ -144,27 +144,17 @@ - (void)dealloc
- (void)addTokenWithTitle:(NSString *)string representedObject:(id)obj
{
NSString *aString = [string stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceCharacterSet]];
- NSMutableString *recipient = [NSMutableString string];
-
- NSMutableCharacterSet *charSet = [[[NSCharacterSet whitespaceCharacterSet] mutableCopy] autorelease];
- [charSet formUnionWithCharacterSet:[NSCharacterSet punctuationCharacterSet]];
-
- for (int i = 0; i < [(NSString *)obj length]; i++)
- {
- if (![charSet characterIsMember:[(NSString *)obj characterAtIndex:i]])
- {
- [recipient appendFormat:@"%@",[NSString stringWithFormat:@"%c", [(NSString *)obj characterAtIndex:i]]];
- }
- }
+ [_textField setText:nil];
+
if ([aString length])
{
- JSTokenButton *token = [self tokenWithString:aString representedObject:recipient];
+ JSTokenButton *token = [self tokenWithString:aString representedObject:obj];
[_tokens addObject:token];
if ([self.delegate respondsToSelector:@selector(tokenField:didAddToken:representedObject:)])
{
- [self.delegate tokenField:self didAddToken:aString representedObject:recipient];
+ [self.delegate tokenField:self didAddToken:aString representedObject:obj];
}
[self setNeedsLayout];
@@ -384,13 +374,11 @@ - (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRang
- (BOOL)textFieldShouldReturn:(UITextField *)textField
{
- if ([[textField text] length])
- {
- [self addTokenWithTitle:[textField text] representedObject:[textField text]];
-
- if (textField == _textField)
- [textField setText:nil];
- }
+ if (_textField == textField) {
+ if ([self.delegate respondsToSelector:@selector(tokenFieldShouldReturn:)]) {
+ return [self.delegate tokenFieldShouldReturn:self];
+ }
+ }
return NO;
}
@@ -399,7 +387,11 @@ - (void)textFieldDidEndEditing:(UITextField *)textField
{
if (textField == _textField)
{
- if ([[textField text] length] > 1)
+ if ([self.delegate respondsToSelector:@selector(tokenFieldDidEndEditing:)]) {
+ [self.delegate tokenFieldDidEndEditing:self];
+ return;
+ }
+ else if ([[textField text] length] > 1)
{
[self addTokenWithTitle:[textField text] representedObject:[textField text]];
[textField setText:ZERO_WIDTH_SPACE_STRING];

0 comments on commit bbfcae4

Please sign in to comment.