iOS奋斗超棒文集iOS 实用

Button多参数点击

2016-12-06  本文已影响372人  无尽思绪

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);  
}];  
上一篇下一篇

猜你喜欢

热点阅读