• 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,完成。

  • 相关阅读:
    request内置对象(上)1
    康拓展开-----两个排列的位置之差
    判断一个数的质因子个数
    学生信息管理系统----(顺序表)
    学生信息管理系统----(链表)
    二分图的最大匹配--匈牙利算法
    hdu-1285拓扑排序
    文件的压缩与解压
    树莓派安装QT(全部库包括)
    Win10 + CLion + 树莓派 + QT 远程开发调用Python
  • 原文地址:https://www.cnblogs.com/yipingios/p/5714595.html
Copyright © 2020-2023  润新知