Created
December 10, 2012 17:22
-
-
Save eraserhd/4251957 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| + (void)load | |
| { | |
| [self textFieldsCycleInThisOrder:@[@"_name", @"_address", @"_phone"]]; | |
| } | |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #import <Foundation/Foundation.h> | |
| @interface NSObject (TabOrder) | |
| + (void)textFieldsCycleInThisOrder:(NSArray *)fieldOrder; | |
| @end |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #import "NSObject+TabOrder.h" | |
| #import <objc/runtime.h> | |
| @interface TabOrderInfo : NSObject | |
| @property (readwrite, retain) NSArray *fieldOrder; | |
| @property (readwrite, assign) IMP oldImplementation; | |
| @end | |
| @implementation TabOrderInfo | |
| @end | |
| static NSMutableDictionary *fieldOrders = nil; | |
| @implementation NSObject (TabOrder) | |
| + (void)textFieldsCycleInThisOrder:(NSArray *)fieldOrder | |
| { | |
| TabOrderInfo *info = [[[TabOrderInfo alloc] init] autorelease]; | |
| info.fieldOrder = fieldOrder; | |
| Method newMethod = class_getInstanceMethod([self class], @selector(TabOrder_textFieldShouldReturn:)); | |
| info.oldImplementation = class_replaceMethod([self class], @selector(textFieldShouldReturn:), method_getImplementation(newMethod), method_getTypeEncoding(newMethod)); | |
| if (!fieldOrders) | |
| fieldOrders = [[NSMutableDictionary alloc] init]; | |
| [fieldOrders setObject:info forKey:NSStringFromClass([self class])]; | |
| } | |
| + (TabOrderInfo *)TabOrder_info | |
| { | |
| return [fieldOrders objectForKey:NSStringFromClass([self class])]; | |
| } | |
| + (NSArray *)TabOrder_fieldsInOrder | |
| { | |
| return [self TabOrder_info].fieldOrder; | |
| } | |
| - (BOOL)TabOrder_textFieldShouldReturn:(UITextField *)textField | |
| { | |
| UIView *nextField = [self TabOrder_fieldAfter:textField]; | |
| if (nextField) | |
| [nextField becomeFirstResponder]; | |
| else | |
| [textField resignFirstResponder]; | |
| IMP oldImplementation = [[self class] TabOrder_info].oldImplementation; | |
| if (oldImplementation) | |
| return ((BOOL (*) (id,SEL, UITextField*))oldImplementation)(self, _cmd, textField); | |
| return NO; | |
| } | |
| - (int)TabOrder_indexOfField:(UIView *)field | |
| { | |
| NSArray *fieldsInOrder = [[self class] TabOrder_fieldsInOrder]; | |
| for (int fieldIndex = 0; fieldIndex < [fieldsInOrder count]; ++fieldIndex) { | |
| if ([self valueForKey:fieldsInOrder[fieldIndex]] == field) | |
| return fieldIndex; | |
| } | |
| return -1; | |
| } | |
| - (UIView *)TabOrder_fieldAfter:(UIView *)field | |
| { | |
| int fieldIndex = [self TabOrder_indexOfField:field]; | |
| if (fieldIndex == -1) | |
| return nil; | |
| if (fieldIndex+1 < [[[self class] TabOrder_fieldsInOrder] count]) | |
| return [self valueForKey:[[self class] TabOrder_fieldsInOrder][fieldIndex+1]]; | |
| return nil; | |
| } | |
| @end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment