iOS 原生的 UIButton 點(diǎn)擊事件是不允許帶多參數(shù)的嘶炭,唯一的一個(gè)參數(shù)就是默認(rèn)UIButton本身
那么我們該怎么實(shí)現(xiàn)傳遞多個(gè)參數(shù)的點(diǎn)擊事件呢裂逐?
1.如果業(yè)務(wù)場景非常簡單歹鱼,要求傳單參數(shù)并且是整數(shù)類型,可以用tag
[cell.deleteButton setTag:indexPath.row]; //例如卜高,將cell的行數(shù)設(shè)置成tag
2.利用ObjC關(guān)聯(lián)弥姻,runtime之所以被稱為iOS 的動(dòng)態(tài)特性是有道理的南片,當(dāng)然關(guān)聯(lián)甚至可以幫助NSArray等其他對象實(shí)現(xiàn)“多參數(shù)傳遞”
實(shí)現(xiàn)起來也非常簡便:
UIButton *btn = // create the button
objc_setAssociatedObject(btn, "firstObject", someObject, OBJC_ASSOCIATION_RETAIN_NONATOMIC); //實(shí)際上就是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.
}
3.利用自定義,添加一個(gè)多參數(shù)的字典屬性變量即可(為什么要字典庭敦?可以裝多多的)
自定義Button子類疼进,甚至都不用重寫啥的:
@interface MultiParamButton : UIButton
@property (nonatomic, strong) NSDictionary* multiParamDic;
@end
傳參:
NSDictionary* paramDic = @{@"one":@"one", @"two":@2, @"third":@(3)};
MultiParamButton* multiParamButton = [[MultiParamButton alloc] init];
[multiParamButton setFrame:CGRectMake(0, 0, 50, 50)];
multiParamButton.center = self.view.center;
[multiParamButton setBackgroundColor:[UIColor grayColor]];
[multiParamButton addTarget:self action:@selector(multiParamButtonClicked:) forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:multiParamButton];
multiParamButton.multiParamDic = paramDic;
點(diǎn)擊:
- (void)multiParamButtonClicked:(UIButton* )button
{
MultiParamButton* multiParamButton = (MultiParamButton* )button;
NSLog(@"Vvvverify : %@", multiParamButton.multiParamDic);
}
爽爽的:
當(dāng)然,如果用擴(kuò)展秧廉,然后添加property后重寫GetSet也是一樣一樣的
4.完全不在Button上入手伞广,針對業(yè)務(wù)來,最常見的就是在TableViewCell上面的Button疼电,這種存在(視圖)繼承樹之間的傳遞嚼锄,這里舉個(gè)簡單的例子
Button獲取所屬父視圖的所屬視圖控制器的參數(shù),間接傳參
#import "LBMultiParamButtonController.h"
#import "MultiParamButton.h"
@interface LBMultiParamButtonController ()
@property (nonatomic, strong) NSDictionary* paramDic;
@end
@implementation LBMultiParamButtonController
- (id)init
{
self = [super init];
if (self)
{
_paramDic = @{@"one":@"one", @"two":@2, @"third":@(3)};
}
return self;
}
- (void)viewDidLoad
{
[super viewDidLoad];
UIButton* button = [UIButton buttonWithType:UIButtonTypeCustom];
[button setFrame:CGRectMake(0, 0, 50, 50)];
[button setCenter:self.view.center];
[button setBackgroundColor:[UIColor grayColor]];
[button addTarget:self action:@selector(buttonClicked:) forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:button];
}
- (void)buttonClicked:(UIButton* )button
{
LBMultiParamButtonController* multiParamButtonController = nil;
//獲取button所屬的視圖控制器蔽豺,如果視圖控制器都能獲取区丑,還有什么不能獲取呢?
for(UIView* next = [button superview]; next; next = next.superview)
{
UIResponder *nextResponder = [next nextResponder];
if ([nextResponder isKindOfClass:[LBMultiParamButtonController class]])
{
multiParamButtonController = (LBMultiParamButtonController* )nextResponder;
break;
}
}
NSLog(@"param : %@", multiParamButtonController.paramDic);
}
@end
這種非常多的用在UITableViewCell上自定義的按鈕的參數(shù)的情況修陡!
5.利用Delegate和performSelector:withObject:withObject 方法可以傳遞最多兩個(gè)參數(shù):
定義protocol:
#pragma mark - SYAccountListCellDelegate.
@class SYAccountListCell;
@protocol SYAccountListCellDelegate <NSObject>
- (void)accountListCell:(SYAccountListCell* )cell didTapButton:(UIButton* )button;
@end
自定義Cell的時(shí)候?qū)⒛阆雮鞯臇|西傳進(jìn)入沧侥,這里用cell和button做例子:
@implementation SYAccountListCell
- (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
if (self)
{
self.deleteButton = [UIButton buttonWithType:UIButtonTypeCustom];
[self.deleteButton setFrame:CGRectMake(225,
5,
40,
40)];
[self.deleteButton setBackgroundColor:[UIColor redColor]];
[self.deleteButton addTarget:self action:@selector(deleteButtonClicked:) forControlEvents:UIControlEventTouchUpInside];
[self.contentView addSubview:self.deleteButton];
}
return self;
}
- (void)deleteButtonClicked:(UIButton* )button
{
if ([self.delegate respondsToSelector:@selector(accountListCell:didTapButton:)])
{
[self.delegate performSelector:@selector(accountListCell:didTapButton:) withObject:self withObject:button];
}
}
@end
Delegate實(shí)現(xiàn):
#pragma mark - SYAccountListCellDelegate.
- (void)accountListCell:(SYAccountListCell *)cell didTapButton:(UIButton *)button
{
NSLog(@"Cell : %@ , Button : %@", cell, button);
}
雖然有點(diǎn)曲折,但是傳參效果非常到位
這里補(bǔ)充一下魄鸦,這里的最多兩個(gè)參數(shù)是直面的參數(shù)個(gè)數(shù)宴杀,如果將參數(shù)設(shè)置位結(jié)構(gòu)體,那么就皆大歡喜啦号杏,想怎么傳就怎么傳婴氮!
6.利用Block 和 關(guān)聯(lián) , 直接可以當(dāng)前點(diǎn)擊并且操作參數(shù) - 強(qiáng)盾致!
#import <UIKit/UIKit.h>
typedef void (^ActionBlock)();
@interface UIButton (Utility)
@property (readonly) NSMutableDictionary *event;
- (void) handleControlEvent:(UIControlEvents)controlEvent withBlock:(ActionBlock)action;
@end
實(shí)現(xiàn)文件:
#import <objc/runtime.h>
#import "UIButton+Utility.h"
@implementation UIButton (Utility)
static char overviewKey;
@dynamic event;
- (void)handleControlEvent:(UIControlEvents)event withBlock:(ActionBlock)block
{
objc_setAssociatedObject(self, &overviewKey, block, OBJC_ASSOCIATION_COPY_NONATOMIC);
[self addTarget:self action:@selector(callActionBlock:) forControlEvents:event];
}
- (void)callActionBlock:(id)sender
{
ActionBlock block = (ActionBlock)objc_getAssociatedObject(self, &overviewKey);
if (block)
{
block();
}
}
@end
操作:
[button handleControlEvent:UIControlEventTouchUpInside withBlock:^{
NSLog(@"ssss : %@", self.paramDic);
}];