- 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.
- }
时间: 2024-10-25 16:58:30