开发中,我们经常需要对View,button,alter 做一些操作,,但是也需要穿相应的参数,使用runtime 可以让这种方式更简单的传递,为了方便使用,封装了一个类别
//.h
@interface UIView (parameter){
}
-(void)setCustomParame:(NSDictionary*)dic;
-(NSDictionary*)customParame;
@end
//.m
@implementation UIView (parameter)
static char UIViewParameterKey;
-(void)setCustomParame:(NSDictionary *)dic{
objc_setAssociatedObject(self, &UIViewParameterKey, dic, OBJC_ASSOCIATION_RETAIN_NONATOMIC);
}
-(NSDictionary*)customParame{
return objc_getAssociatedObject(self, &UIViewParameterKey);
}
@end
因为UIButton,UILabel等都是继承UIView的,所以是添加在UIView 上添加的category,这样就可以直接调用即可。