zoukankan      html  css  js  c++  java
  • 利用KVC实现无需协议的委托模式

    在《精通iOS开发》一书中看到的技巧。假设BIDTaskListController是一个列表,点击列表上的一项将会导航到BIDTaskDetailController,在BIDTaskDetailController当中修改值并保存后,将把修改后的值回传给BIDTaskListController并更新局部视图。

    在BIDTaskListController类中有如下方法:

     1 - (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
     2     UIViewController *destination = segue.destinationViewController;
     3     if ([destination respondsToSelector:@selector(setDelegate:)]) {
     4         [destination setValue:self forKey:@"delegate"];
     5     }
     6     if ([destination respondsToSelector:@selector(setSelection:)]) {
     7         // prepare selection info
     8         NSIndexPath *indexPath = [self.tableView indexPathForCell:sender];
     9         id object = self.tasks[indexPath.row];
    10         NSDictionary *selection = @{@"indexPath" : indexPath,
    11         @"object" : object};
    12         [destination setValue:selection forKey:@"selection"];
    13     }
    14 }

    红字部分通过segue拿到了目标控制器,并且通过KVC的方式将自身设置为了目标的代理。

    在BIDTaskDetailController当中有如下回调:

    1 [self.delegate setValue:editedSelection forKey:@"editedSelection"];

    这句代码调用了BIDTaskListController当中editedSelection属性的setter方法,实现了对数据源的修改,并且在setter方法中刷新了视图:

     1 - (void)setEditedSelection:(NSDictionary *)dict {
     2     if (![dict isEqual:self.editedSelection]) {
     3         _editedSelection = dict;
     4         NSIndexPath *indexPath = dict[@"indexPath"];
     5         id newValue = dict[@"object"];
     6         [self.tasks replaceObjectAtIndex:indexPath.row withObject:newValue];
     7         [self.tableView reloadRowsAtIndexPaths:[NSArray arrayWithObject:indexPath]
     8                               withRowAnimation:UITableViewRowAnimationAutomatic];
     9     }
    10 }

    如此,借助KVC以及回调数据源的setter方法可以彻底解除两个控制器之间的耦合,同时避免了定义协议,使代码更灵活。

  • 相关阅读:
    HTML 基础学习笔记
    使用python访问网络上的数据
    linux上创建ftp服务器下载文件///使用AWS服务器作为代理,下载sbt相关的包
    jquery,字符串转json对象,json对象转字符串
    vue渲染过程的{{xxx}}显示的解决办法
    v-model select 选中值后自动改变vue实例中的属性值
    uniapp华为手机真机运行方法
    获取本机mac地址
    编码Code
    int型比较大小
  • 原文地址:https://www.cnblogs.com/Steak/p/3577145.html
Copyright © 2011-2022 走看看