zoukankan      html  css  js  c++  java
  • 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,完成。

  • 相关阅读:
    适配器
    装饰器
    getOwnPropertyDescriptor
    发布订阅
    策略模式
    window.requestAnimationFrame() 和 window.cancelAnimationFrame()
    L1-056 猜数字
    L1-055 谁是赢家
    L1-054 福到了
    L1-053 电子汪
  • 原文地址:https://www.cnblogs.com/yipingios/p/5714595.html
Copyright © 2011-2022 走看看