iOS原生的 UIButton 点击事件是不允许带多参数的,唯一的一个参数就是默认UIButton本身
那么我们该怎么实现传递多个参数的点击事件呢?
1.如果业务场景非常简单,要求传单参数并且是整数类型,可以用tag
[cell.deleteButtonsetTag:indexPath.row];//例如,将cell的行数设置成tag
2.利用ObjC关联,runtime之所以被称为iOS 的动态特性是有道理的,当然关联甚至可以帮助NSArray等其他对象实现“多参数传递”
实现起来也非常简便:
UIButton*btn =// create the button
objc_setAssociatedObject(btn,"firstObject", someObject, OBJC_ASSOCIATION_RETAIN_NONATOMIC);//实际上就是KVC
objc_setAssociatedObject(btn,"secondObject", otherObject, OBJC_ASSOCIATION_RETAIN_NONATOMIC);
[btnaddTarget:selfaction:@selector(click:)forControlEvents:UIControlEventTouchUpInside];
- (void)click:(UIButton*)sender
{
idfirst = objc_getAssociatedObject(btn,"firstObject");//取参
idsecond = objc_setAssociatedObject(btn,"secondObject");
// etc.
}
3.利用自定义,添加一个多参数的字典属性变量即可(为什么要字典?可以装多多的)
自定义Button子类,甚至都不用重写啥的:
@interfaceMultiParamButton : UIButton
@property(nonatomic,strong) NSDictionary* multiParamDic;
@end
传参:
NSDictionary* paramDic = @{@"one":@"one",@"two":@2,@"third":@(3)};
MultiParamButton* multiParamButton = [[MultiParamButtonalloc]init];
[multiParamButtonsetFrame:CGRectMake(0,0,50,50)];
multiParamButton.center=self.view.center;
[multiParamButtonsetBackgroundColor:[UIColorgrayColor]];
[multiParamButtonaddTarget:selfaction:@selector(multiParamButtonClicked:)forControlEvents:UIControlEventTouchUpInside];
[self.viewaddSubview:multiParamButton];
multiParamButton.multiParamDic= paramDic;
点击:
- (void)multiParamButtonClicked:(UIButton* )button
{
MultiParamButton* multiParamButton = (MultiParamButton* )button;
NSLog(@"Vvvverify : %@", multiParamButton.multiParamDic);
}
当然,如果用扩展,然后添加property后重写GetSet也是一样一样的
4.完全不在Button上入手,针对业务来,最常见的就是在TableViewCell上面的Button,这种存在(视图)继承树之间的传递,这里举个简单的例子
Button获取所属父视图的所属视图控制器的参数,间接传参
#import "LBMultiParamButtonController.h"
#import "MultiParamButton.h"
@interfaceLBMultiParamButtonController ()
@property(nonatomic,strong) NSDictionary* paramDic;
@end
@implementationLBMultiParamButtonController
- (id)init
{
self= [superinit];
if(self)
{
_paramDic = @{@"one":@"one",@"two":@2,@"third":@(3)};
}
returnself;
}
- (void)viewDidLoad
{
[superviewDidLoad];
UIButton* button = [UIButtonbuttonWithType:UIButtonTypeCustom];
[buttonsetFrame:CGRectMake(0,0,50,50)];
[buttonsetCenter:self.view.center];
[buttonsetBackgroundColor:[UIColorgrayColor]];
[buttonaddTarget:selfaction:@selector(buttonClicked:)forControlEvents:UIControlEventTouchUpInside];
[self.viewaddSubview:button];
}
- (void)buttonClicked:(UIButton* )button
{
LBMultiParamButtonController* multiParamButtonController =nil;
//获取button所属的视图控制器,如果视图控制器都能获取,还有什么不能获取呢?
for(UIView* next = [buttonsuperview]; next; next = next.superview)
{
UIResponder*nextResponder = [nextnextResponder];
if([nextResponderisKindOfClass:[LBMultiParamButtonControllerclass]])
{
multiParamButtonController = (LBMultiParamButtonController* )nextResponder;
break;
}
}
NSLog(@"param : %@", multiParamButtonController.paramDic);
}
@end
这种非常多的用在UITableViewCell上自定义的按钮的参数的情况!
5.利用Delegate和performSelector:withObject:withObject 方法可以传递最多两个参数:
定义protocol:
#pragma mark - SYAccountListCellDelegate.
@classSYAccountListCell;
@protocolSYAccountListCellDelegate
- (void)accountListCell:(SYAccountListCell* )celldidTapButton:(UIButton* )button;
@end
自定义Cell的时候将你想传的东西传进入,这里用cell和button做例子:
@implementationSYAccountListCell
- (id)initWithStyle:(UITableViewCellStyle)stylereuseIdentifier:(NSString*)reuseIdentifier
{
self= [superinitWithStyle:stylereuseIdentifier:reuseIdentifier];
if(self)
{
self.deleteButton= [UIButtonbuttonWithType:UIButtonTypeCustom];
[self.deleteButtonsetFrame:CGRectMake(225,
5,
40,
40)];
[self.deleteButtonsetBackgroundColor:[UIColorredColor]];
[self.deleteButtonaddTarget:selfaction:@selector(deleteButtonClicked:)forControlEvents:UIControlEventTouchUpInside];
[self.contentViewaddSubview:self.deleteButton];
}
returnself;
}
- (void)deleteButtonClicked:(UIButton* )button
{
if([self.delegaterespondsToSelector:@selector(accountListCell:didTapButton:)])
{
[self.delegateperformSelector:@selector(accountListCell:didTapButton:)withObject:selfwithObject:button];
}
}
@end
Delegate实现:
#pragma mark - SYAccountListCellDelegate.
- (void)accountListCell:(SYAccountListCell*)celldidTapButton:(UIButton*)button
{
NSLog(@"Cell : %@ , Button : %@", cell, button);
}
虽然有点曲折,但是传参效果非常到位
这里补充一下,这里的最多两个参数是直面的参数个数,如果将参数设置位结构体,那么就皆大欢喜啦,想怎么传就怎么传!
6.利用Block 和 关联 , 直接可以当前点击并且操作参数 - 强!
#import
typedefvoid(^ActionBlock)();
@interfaceUIButton (Utility)
@property(readonly)NSMutableDictionary*event;
- (void)handleControlEvent:(UIControlEvents)controlEventwithBlock:(ActionBlock)action;
@end
实现文件:
#import
#import "UIButton+Utility.h"
@implementationUIButton (Utility)
staticcharoverviewKey;
@dynamicevent;
- (void)handleControlEvent:(UIControlEvents)eventwithBlock:(ActionBlock)block
{
objc_setAssociatedObject(self, &overviewKey, block, OBJC_ASSOCIATION_COPY_NONATOMIC);
[selfaddTarget:selfaction:@selector(callActionBlock:)forControlEvents:event];
}
- (void)callActionBlock:(id)sender
{
ActionBlock block = (ActionBlock)objc_getAssociatedObject(self, &overviewKey);
if(block)
{
block();
}
}
@end
操作:
[buttonhandleControlEvent:UIControlEventTouchUpInsidewithBlock:^{
NSLog(@"ssss : %@",self.paramDic);
}];