Add runtime variables to UI elements which inherit from UIControl/NSControl

Viewed 39

I've noticed that there's an option to bind runtime attribute for NSButton or any user UI element as shown in the example below. But how can I access this attribute from code for get/set operations ?

enter image description here

I've read that I cannot initialize attribute that isn't part of the class, but if I'll define category for NSControl which is inherited by NSButton class with runtime setter and getter to xxx, would it be possible :

@implementation NSControl (withXXX)
@dynamic xxx;

- (void)setXxx:(id)object {    
    objc_setAssociatedObject(self, @selector(xxx), object, OBJC_ASSOCIATION_RETAIN_NONATOMIC);
}

- (id)xxx {    
    return objc_getAssociatedObject(self, @selector(xxx));
}
0 Answers
Related