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
    评论
引用\[2\]中提到,使用std::ios::ate打开文件时,文件指针会定位到文件末尾。然而,如果不配合std::ios::in模式,即只使用std::ios::ate和std::ios::out模式打开文件,会清空原文件。所以,std::ios::ate并不能实现追加写入的功能。如果想要实现追加写入,可以使用std::ios::app模式打开文件,这样写入的内容会被追加到文件末尾。 #### 引用[.reference_title] - *1* [C++ 文件读写操作std::ofstream和std::ifstream](https://blog.csdn.net/block999123/article/details/121869208)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^control,239^v3^insert_chatgpt"}} ] [.reference_item] - *2* [ofstream之ios::ate,ios::app,ios::in,ios::out](https://blog.csdn.net/cabbage2008/article/details/53307409)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^control,239^v3^insert_chatgpt"}} ] [.reference_item] - *3* [std::ifstream是C++标准库中的一个输入文件流类,它提供了一组函数来读取文件中的数据](https://blog.csdn.net/m0_46661183/article/details/130769483)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^control,239^v3^insert_chatgpt"}} ] [.reference_item] [ .reference_list ]

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

sailip

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

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

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

打赏作者

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

抵扣说明:

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

余额充值