.h @interface UIView (ObjectTagAdditions) @property (nonatomic, retain) id objectTag; @end
.m #import <objc/runtime.h> static char const * const ObjectTagKey = "ObjectTag"; @implementation UIView (ObjectTagAdditions) @dynamic objectTag; - (id)objectTag { return objc_getAssociatedObject(self, &ObjectTagKey); } - (void)setObjectTag:(id)newObjectTag { objc_setAssociatedObject(self, &ObjectTagKey, newObjectTag, OBJC_ASSOCIATION_RETAIN_NONATOMIC); } Cited from : http://oleb.net/blog/2011/05/faking-ivars-in-objc-categories-with-associative-references/
Advertisements
February 5, 2015 at 8:33 am
If you copy the UIView instance that has objectTag value to new instance. The new instance won’t hold the objectTag value, so manually set the objectTag value.
UIView newView = [oldView copy];
newView.objectTag = oldView.objectTag;