In Notethread I'm developing a tree view to display all notes. I want to attach the Note model to a UIButton, so when the user taps the button it can easily get the correct note data.
This StackOverflow answer helped me do that:
#import <objc/runtime.h>
/*
So I can assign a Note per button
http://stackoverflow.com/a/5287141
*/
@interface UIButton (NoteModel)
@property (nonatomic, strong) Note *note;
@end
@implementation UIButton (NoteModel)
static char noteKey;
- (void)setNote:(Note *)note {
objc_setAssociatedObject( self, ¬eKey, note, OBJC_ASSOCIATION_RETAIN );
}
- (Note *)note {
return objc_getAssociatedObject(self, ¬eKey);
}
@end
What we get is a fairly simple Category. I'm not sure if it's the best solution, but I think it's more elegant than tagging an index number for an array; which was my initial thought.