iOS 設計模式-Block實現代理的邏輯


在A頁面,點擊跳轉到B頁面,B頁面操作完,回到A頁面,並刷新A頁面的內容。典型的例子,就是在一個列表里,點擊新增,跳到新增頁面,新增完,把數據傳回給列表頁,並刷新列表頁里的內容。

這個,我平時一般是通過代理來實現,下面試着通過Block來實現。

在B頁面定義Block,供A頁面調用。

/**
 *  確認訂單選擇返回block
 */
@property (nonatomic, strong) void (^ selectedAddressBlock)(HGAddressModel * address);

B頁面,操作完成,給Block傳回調值

/**
 *  選中行,為了確認訂單時,選擇收貨地址使用
 */
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    HGAddressModel *entity = self.dataSource[indexPath.row];
    if (self.selectedAddressBlock) {
        self.selectedAddressBlock(entity);
    }
    [self.navigationController popViewControllerAnimated:YES];
}

A頁面操作就很簡單了,跳轉到B頁面,直接調用B頁面的Block 就可以拿到結果了。

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

OK,完成。

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM