- UIButton *btn = // create the button
- objc_setAssociatedObject(btn, "firstObject", someObject, OBJC_ASSOCIATION_RETAIN_NONATOMIC); //实际上就是KVC
- objc_setAssociatedObject(btn, "secondObject", otherObject, OBJC_ASSOCIATION_RETAIN_NONATOMIC);
- [btn addTarget:self action:@selector(click:) forControlEvents:UIControlEventTouchUpInside];
- - (void)click:(UIButton *)sender
- {
- id first = objc_getAssociatedObject(btn, "firstObject"); //取参
- id second = objc_setAssociatedObject(btn, "secondObject");
- // etc.
- }