前言
点击按钮的时候想传一些值过去,但是在按钮的实现方法里面只能拿到sender的一些数据。没法拿到其他数据。
下面介绍怎么使用:
self.btn_yyh = [[UIButton alloc]init];```
self.btn_yyh.backgroundColor=KYRGBColor(108,73,191);
self.btn_yyh.layer.cornerRadius = 12.5f;
[self.btn_yyh setTitle:@"上传运单号" forState:UIControlStateNormal];
self.btn_yyh.titleLabel.font = [UIFont systemFontOfSize: 12.0];
[self.btn_yyh setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
[self.btn_yyh addTarget:self action:@selector(touchYYHBtn:) forControlEvents:UIControlEventTouchUpInside];
//以下为传值的方法
objc_setAssociatedObject(self.btn_yyh, @"oid",orderInfoModel.orderId, OBJC_ASSOCIATION_RETAIN_NONATOMIC);
objc_setAssociatedObject(self.btn_yyh, @"goodsAddress",orderInfoModel.addressId, OBJC_ASSOCIATION_RETAIN_NONATOMIC);
[view_faddSubview:self.btn_yyh];
[self.btn_yyh mas_makeConstraints:^(MASConstraintMaker *make) {
make.width.mas_equalTo(77.0f);
make.height.mas_equalTo(22.0f);
make.right.mas_equalTo(view_f).offset(-space);
make.centerY.mas_equalTo(self.label_date);
}];
// 取值
- (void)touchYYHBtn:(UIButton*)sender
{
NSLog(@"上传运单号");
KYSCYDHVC*scydVC = [[KYSCYDHVCalloc]init];
NSString *oid = objc_getAssociatedObject(sender, @"oid");
NSString *goodsAddress = objc_getAssociatedObject(sender, @"goodsAddress");
scydVC.goodsAddress= goodsAddress;
scydVC.orderId= oid;
[self.navigationController pushViewController:scydVC animated:YES];
}