iOS设计模式-Block实现代理的逻辑

在A页面,点击跳转到B页面,B页面操作完,回到A页面,并刷新A页面的内容。典型的例子,就是在一个列表里,点击新增,跳到新增页面,新增完,把数据传回给列表页,并刷新列表页里的内容。

这个,我平时一般是通过代理来实现,下面试着通过Block来实现。

在B页面定义Block,供A页面调用

1 /**
2  *  确认订单选择返回block
3  */
4 @property (nonatomic, strong) void (^ selectedAddressBlock)(HGAddressModel * address);

B页面,操作完成,给Block传回调值

 1 /**
 2  *  选中行,为了确认订单时,选择收货地址使用
 3  */
 4 - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
 5 {
 6     HGAddressModel *entity = self.dataSource[indexPath.row];
 7     if (self.selectedAddressBlock) {
 8         self.selectedAddressBlock(entity);
 9     }
10     [self.navigationController popViewControllerAnimated:YES];
11 }

A页面操作就很简单了,跳转到B页面,直接调用B页面的Block 就可以拿到结果了。

1 - (void)jumpAddress:(UIButton *)sender {
2     AddressListViewController *address = [[AddressListViewController alloc] init];
3     address.hidesBottomBarWhenPushed = YES;
4     address.title = @"地址管理";
5     address.selectedAddressBlock = ^(HGAddressModel *model){
6         DR_NSLog(@"----------model------------%@",model.province_name);
7     };
8     [self.navigationController pushViewController:address animated:YES];
9 }

OK,完成。

 

转载于:https://www.cnblogs.com/yipingios/p/5714595.html

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值