(Turn) [IOS] button click event pass value

Reprinted from: http://blog.csdn.net/LOLITA0164/article/details/76019938

 

1. The emergence of problems

The only parameter of the click event of the native UIButton is the UIButton itself. We usually use the tag that comes with the UIButton to use different parameters. In simple business scenarios, the tag can meet the needs, but in some complex businesses In this case, the tag seems a little powerless. After all, passing the parameters of the click event through the tag is only an indirect way, and does not bind the data source to the control. For example, the table view has multiple sections, and there are multiple btn on the cell, so how to get the corresponding data for the click event of btn? In fact, it is possible to pass tags, but it will become a bit complicated.


So why not get the data through direct association? The following are the solutions for passing parameters of btn.


Second, pass parameters

1), get data indirectly

a. Through the tag of btn (applicable to most businesses)

// 设置tag
btn.tag = indexPath.row;
[btn addTarget:self action:@selector(btnAction:) forControlEvents:UIControlEventTouchUpInside];
// 通过tag获取数据
-(void)btnAction:(UIButton *)btn{
    NSLog(@"%@",self.data[btn.tag]);
}

b, through the parent view of btn

这种方式需要将数据源绑定到父视图上,当点击btn时,通过父视图来获取数据
业务应用:一个cell上有多个按钮,共用cell的数据时

2), get data directly (association binding)

a, custom btn (inheritance), new attributes

Add an attribute directly to btn as a parameter, such as a dictionary

@interface MyButton : UIButton
@property (strong ,nonatomic) NSDictionary *paramDic; // 用来传递参数
@end

// 直接赋值
btn.paramDic = @{@"name":@"LOLITA",@"age":@"24"};

-(void)btnAction:(MyButton *)btn{
    NSLog(@"%@",btn.paramDic);
}

b, category / classification, new attributes

If you don't want to pass parameters by custom btn, you can add a property to your UIButton by category

Step 1. Create a new UIButton category and add an attribute

@interface UIButton (PassValue)
@property (strong ,nonatomic) NSDictionary *paramDic;
@end

Step 2. Implement setter and getter methods

-(NSDictionary *)paramDic{
    return objc_getAssociatedObject(self, _cmd);
}
-(void)setParamDic:(NSDictionary *)paramDic{
    objc_setAssociatedObject(self, @selector(paramDic), paramDic, OBJC_ASSOCIATION_RETAIN_NONATOMIC);
}

Step 3. Import the file and use the new properties of btn

UIButton *btn = [[UIButton alloc] initWithFrame:CGRectMake(0, 0, 200, 100)];
btn.paramDic = @{@"name":@"LOLITA",@"age":@"24"};

-(void)btnAction:(MyButton *)btn{
    NSLog(@"%@",btn.paramDic); 
}

c, dynamic runtime binding data

In the previous method, we have used the dynamic runtime to implement the setter and getter methods of btn to add properties to the classification. Then, if you do not want to create a new classification file of btn, you can directly use the runtime to bind data.

#import <objc/runtime.h>

 

// 绑定数据源
objc_setAssociatedObject(btn, @"myBtn", dataDic, OBJC_ASSOCIATION_RETAIN_NONATOMIC);

 

 

-(void)yanbaoClick:(UIButton *)sender{
    // get the data source
    NSDictionary *dic = objc_getAssociatedObject(sender, @"myBtn");
    NSLog(@"%@",dic);
 

 

 

 

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=326134264&siteId=291194637