iOS 原生的 UIButton 點擊事件是不允許帶多參數的,唯一的一個參數就是默認UIButton本身
那麼我們該怎麼實現傳遞多個參數的點擊事件呢?
1.如果業務場景非常簡單,要求傳單參數並且是整數類型,可以用tag
[cell.deleteButton setTag: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); [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.利用自定義,添加一個多參數的字典屬性變量即可(為什麼要字典?可以裝多多的)
自定義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;
點擊:
- (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" @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上自定義的按鈕的參數的情況!
5.利用Delegate和performSelector:withObject:withObject 方法可以傳遞最多兩個參數:
定義protocol:
#pragma mark - SYAccountListCellDelegate. @class SYAccountListCell; @protocol SYAccountListCellDelegate- (void)accountListCell:(SYAccountListCell* )cell didTapButton:(UIButton* )button; @end
自定義Cell的時候將你想傳的東西傳進入,這裡用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實現:
#pragma mark - SYAccountListCellDelegate. - (void)accountListCell:(SYAccountListCell *)cell didTapButton:(UIButton *)button { NSLog(@"Cell : %@ , Button : %@", cell, button); }
雖然有點曲折,但是傳參效果非常到位
這裡補充一下,這裡的最多兩個參數是直面的參數個數,如果將參數設置位結構體,那麼就皆大歡喜啦,想怎麼傳就怎麼傳!
6.利用Block 和 關聯 , 直接可以當前點擊並且操作參數 - 強!
#importtypedef void (^ActionBlock)(); @interface UIButton (Utility) @property (readonly) NSMutableDictionary *event; - (void) handleControlEvent:(UIControlEvents)controlEvent withBlock:(ActionBlock)action; @end
實現文件:
#import#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); }];