Button多参数点击

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 <NSObject>  
  
- (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 和 关联 , 直接可以当前点击并且操作参数 - 强!

#import <UIKit/UIKit.h>  
  
typedef void (^ActionBlock)();  
  
@interface UIButton (Utility)  
  
@property (readonly) NSMutableDictionary *event;  
  
- (void) handleControlEvent:(UIControlEvents)controlEvent withBlock:(ActionBlock)action;  
  
@end  

实现文件:

#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);  
}];  
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • Android 自定义View的各种姿势1 Activity的显示之ViewRootImpl详解 Activity...
    passiontim阅读 173,705评论 25 709
  • 随着离去的脚步, 雪花也为你飞舞。 前途的迷路 有谁为你解忧 任雪花潇潇洒洒 也无意阻挡韩流 前途的迷路 只有自己...
    纯真的云卷云舒_6阅读 130评论 0 2
  • 我们之间是没法交流的,而我很清楚问题不在我身上,或许我确实缺乏一些耐心。可我无法理解她不能用一点点的时间来听我把事...
    灯塔高处阅读 159评论 0 0
  • 这本书的作者被《快公司》誉为“商业领域最具创意的一百人”和“推特上最具创意的十位博主”之一。她的绝招是涂鸦,通过“...
    朱桃子阅读 304评论 2 1
  • 类主构造器 主构造器的定义与类的定义交织在一直,将构造器参数直接放在类名称之后,如下代码: 主构造器还可以使用默认...
    LuciferTM阅读 295评论 0 0