前言
點(diǎn)擊按鈕的時(shí)候想傳一些值過(guò)去赋除,但是在按鈕的實(shí)現(xiàn)方法里面只能拿到sender的一些數(shù)據(jù)瞭恰。沒(méi)法拿到其他數(shù)據(jù)竣蹦。
下面介紹怎么使用:
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:@"上傳運(yùn)單號(hào)" 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(@"上傳運(yùn)單號(hào)");
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];
}