Created
September 12, 2012 18:00
-
-
Save cconstable/3708643 to your computer and use it in GitHub Desktop.
UITextField category that let's "next text fields" be assigned via interface builder.
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 <UIKit/UIKit.h> | |
#import <objc/runtime.h> | |
/** | |
Adds a property called "nextTextField" to the UITextField class. | |
This property can be assigned in IB in a very natural way. | |
To make this work, something like this should be added to the VC | |
that is delegate of the UITextFields: | |
- (BOOL)textFieldShouldReturn:(UITextField *)textField | |
{ | |
UITextField *next = [textField nextTextField]; | |
if (next) { | |
[next becomeFirstResponder]; | |
} | |
else { | |
// do something... | |
} | |
return NO; | |
} | |
This class uses objc_getAssociatedObject and objc_setAssociatedObject. | |
More information on objc_getAssociatedObject here: | |
https://developer.apple.com/library/ios/#documentation/Cocoa/Conceptual/ObjectiveC/Chapters/ocAssociativeReferences.html#//apple_ref/doc/uid/TP30001163-CH24-SW1 | |
*/ | |
@interface UITextField (Tabbable) | |
@property(strong, nonatomic) IBOutlet UITextField *nextTextField; | |
@end | |
// This is used | |
static char associationKey; | |
@implementation UITextField (Tabbable) | |
- (UITextField*) nextTextField { | |
return objc_getAssociatedObject(self, &associationKey); | |
} | |
- (void) setNextTextField:(UITextField *)nextTextField{ | |
objc_setAssociatedObject(self, &associationKey, nextTextField, OBJC_ASSOCIATION_RETAIN_NONATOMIC); | |
} | |
@end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment