NSString *firsrString = @"我是第一个按钮";
NSString *secondString = @"我是第二个按钮";
UIButton *btn = [[UIButton alloc] initWithFrame:CGRectMake(100, 100, 150, 50)];
[btn setBackgroundColor:[UIColor redColor]];
[btn setTitle:@"按钮传值" forState:UIControlStateNormal];
[self.view addSubview:btn];
objc_setAssociatedObject(btn,"firstObject",firsrString,OBJC_ASSOCIATION_RETAIN_NONATOMIC); objc_setAssociatedObject(btn, "secondObject", secondString, OBJC_ASSOCIATION_RETAIN_NONATOMIC);
[btn addTarget:self action:@selector(click:) forControlEvents:UIControlEventTouchUpInside];
- (void)click:(UIButton *)sender{
id first = objc_getAssociatedObject(sender, "firstObject");
id second = objc_getAssociatedObject(sender, "secondObject");
NSLog(@"%@--%@", first, second);
}