转自http://blog.csdn.net/leonpengweicn/article/details/8226981
UIView+DHStyleManager.h
#import <UIKit/UIKit.h> #include <objc/runtime.h> #include <objc/objc-runtime.h> @interface UIView (DHStyleManager) @property(nonatomic,copy)NSString *styleName; @end
UIView+DHStyleManager.m
static char KDHStyleKey; #import "UIView+DHStyleManager.h" @implementation UIView (DHStyleManager) @dynamic styleName; -(void)setStyleName:(NSString *)aStyleName { objc_setAssociatedObject(self,&KDHStyleKey,aStyleName,OBJC_ASSOCIATION_COPY); } -(NSString *)styleName { return objc_getAssociatedObject(self, &KDHStyleKey); } @end
AppDelegate.m
UIView *v = [[UIView alloc]init]; v.styleName = @"someStyleName"; NSLog(@"v = %@",v.styleName);