导入头文件,设置key
#import <objc/runtime.h>
// 关键字
static char ReMarkKey;
绑定obj - key - value
UILabel * label3 =[[UILabel alloc]initWithFrame:CGRectMake(0, 60, 100, 20)];
label3.text = @"第3个label";
label3.userInteractionEnabled = YES;
[self addSubview:label3];
/**
* 开始绑定,把label3和value(label3.text)绑定
*/
objc_setAssociatedObject(label3, &ReMarkKey, label3.text, OBJC_ASSOCIATION_RETAIN);
UITapGestureRecognizer * remarkTap =[[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(tapRemark:)];
[label3 addGestureRecognizer:remarkTap];
取值,根据Key来取
-(void)tapRemark:(UITapGestureRecognizer *)sender
{
UILabel * labl = (UILabel *)sender.view;
//根据绑定的label3取value
NSString * remark = objc_getAssociatedObject(labl, &ReMarkKey);
if (self.click) {
self.click(remark); //回调click
}
}