iOS开发:使用响应链方法实现传值及事件传递

引言:

现在有一个需求, 如果一个自定义cell中有一个button, button的点击事件要将自定义cell中的某个属性值传给控制器, 应该怎么做?

当然你可以利用代理, 通知, 和block回调, 除此之外, 还有没有其他办法呢? 有! 那就是今天要说的路由响应链方法,避免了重复代码及代码美观性

直接上代码

UIResponder的类目:


@interface UIResponder (YJRouter)

- (void)routerWithEventName:(NSString *)eventName modelInfo:(NSDictionary *)modelInfo;

@end

 <p>#import "UIResponder+Router.h"

@implementation UIResponder (Router)

- (void)routerWithEventName:(NSString *)eventName userInfo:(NSDictionary *)userInfo {
    if (self.nextResponder) {
         [[self nextResponder] routerWithEventName:eventName userInfo:userInfo];
    }
}

@end

Cell类:

#import <UIKit/UIKit.h>
#import "YJButtonModel.h"

static NSString *const TransferNameEvent = @"TransferNameEvent";
static NSString *const ButtonTag = @"ButtonTag";
static NSString *const ModelInfo = @"ModelInfo";

@interface YJButtonTableViewCell : UITableViewCell

@property (strong, nonatomic) YJButtonModel *model;

@end#import "YJButtonTableViewCell.h"

@interface YJButtonTableViewCell ()

@property (strong, nonatomic) UIButton *sureButton;
@property (strong, nonatomic) UIButton *deleteButton;

@property (strong, nonatomic) YJButtonModel *modelInfo;

@end

@implementation YJButtonTableViewCell

- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
    if ([super initWithStyle:style reuseIdentifier:reuseIdentifier]) {

        [self setupViews];
    }
    return self;
}

- (void)setupViews
{
    self.sureButton = [UIButton buttonWithType:UIButtonTypeCustom];
    [self.sureButton setTitle:@"确认" forState:UIControlStateNormal];
    self.sureButton.backgroundColor = [UIColor greenColor];
    [self.sureButton addTarget:self action:@selector(buttonAction:) forControlEvents:UIControlEventTouchUpInside];
    [self.contentView addSubview:self.sureButton];

    self.deleteButton = [UIButton buttonWithType:UIButtonTypeCustom];
    [self.deleteButton setTitle:@"删除" forState:UIControlStateNormal];
    self.deleteButton.backgroundColor = [UIColor redColor];
    [self.deleteButton addTarget:self action:@selector(buttonAction:) forControlEvents:UIControlEventTouchUpInside];

    [self.contentView addSubview:self.deleteButton];

    [self.sureButton mas_makeConstraints:^(MASConstraintMaker *make) {

        make.centerY.equalTo(self.contentView.mas_centerY);
        make.right.equalTo(self.contentView.mas_right).offset(-20);

    }];

    [self.deleteButton mas_makeConstraints:^(MASConstraintMaker *make) {

        make.centerY.equalTo(self.contentView.mas_centerY);
        make.right.equalTo(self.sureButton.mas_left).offset(-20);


    }];
}

//赋值操作
- (void)setModel:(YJButtonModel *)model
{
    self.modelInfo = model;
    self.textLabel.text = model.title;
}

//按钮点击事件
- (void)buttonAction:(UIButton *)sender
{
    [sender routerWithEventName:TransferNameEvent modelInfo:@{ButtonTag:sender.titleLabel.text, ModelInfo:self.modelInfo}];
}

Controller中主要方法:

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {

    YJButtonTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:ButtonCell forIndexPath:indexPath];

    cell.model = self.dataArray[indexPath.row];
    // Configure the cell...

    return cell;
}

//操作按钮
- (void)routerWithEventName:(NSString *)eventName modelInfo:(NSDictionary *)modelInfo
{
    if ([eventName isEqualToString:TransferNameEvent]) {

        if ([modelInfo[ButtonTag] isEqualToString:@"确认"]) {

            NSLog(@"确认:%@", [modelInfo[ModelInfo] title]);

        } else {

            NSLog(@"删除:%@", [modelInfo[ModelInfo] title]);

            //移除model对象
            [self.dataArray removeObject:modelInfo[ModelInfo]];
            //刷新
            [self.baseTableView reloadData];

        }
    }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

sailip

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值