This is very easy using Objective-C associated objects:
In your header:
@interface UIButton (MyCustomProperty)
@property (readwrite, retain, nonatomic) id myCustomProperty;
@end
In your implementation file:
#include <objc/runtime.h>
/* Associated objects need a unique memory location to use as a key. */
static char MyCustomPropertyKey = 0;
@implementation UIButton (MyCustomProperty)
/* Use @dynamic to tell the compiler you're handling the accessors yourself. */
@dynamic myCustomProperty;
- (id)myCustomProperty {
return objc_getAssociatedObject(self, &MyCustomPropertyKey);
}
- (void)setMyCustomProperty: (id)anObject {
objc_setAssociatedObject(self, &MyCustomPropertyKey, anObject, OBJC_ASSOCIATION_RETAIN_NONATOMIC);
}
@end
That's it!