Skip to content

Instantly share code, notes, and snippets.

@cconstable
Created September 12, 2012 18:00
Show Gist options
  • Save cconstable/3708643 to your computer and use it in GitHub Desktop.
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.
#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