iOS Block、Delegate使用场景

Block

  • 临时性的,只会调用一次的
  • 回调函数较少,比如只需要:完成状态、是否报错、加载进度
优点:
  1. 代码简洁,使用简单
缺点:
  1. 使用不当容易造成循环引用
  2. 对局部变量进行写操作时,需要添加__block
// AFNetworking 示例
AFURLSessionManager *manager = [[AFURLSessionManager alloc] initWithSessionConfiguration:[NSURLSessionConfiguration defaultSessionConfiguration]];

NSURLSessionUploadTask *uploadTask;
uploadTask = [manager
              uploadTaskWithStreamedRequest:request
              progress:^(NSProgress * _Nonnull uploadProgress) {
                  // This is not called back on the main queue.
                  // You are responsible for dispatching to the main queue 
                  });
              }
              completionHandler:^(NSURLResponse * _Nonnull response, id  _Nullable responseObject, NSError * _Nullable error) {
                  if (error) {
                      NSLog(@"Error: %@", error);
                  } else {
                      NSLog(@"%@ %@", response, responseObject);
                  }
              }];
// MJRefresh 示例
self.tableView.mj_footer = [MJRefreshAutoNormalFooter footerWithRefreshingBlock:^{
    //Call this Block When enter the refresh status automatically
}];
// UIView 动画示例
[UIView animateWithDuration:2
                      delay:2
     usingSpringWithDamping:.5
      initialSpringVelocity:.5
                    options:UIViewAnimationOptionRepeat
                animations:^{
   
} completion:^(BOOL finished) {   

}];

Delegate

  • 回调函数较多,并且会多次调用
  • 存在一定的顺序
优点:
  1. 减少代码耦合性
  2. 流程、逻辑较为清晰,维护成本相对较低
缺点:
  1. 因为声明和实现是分开的,可能在代码阅读需要来回切换查找,相对麻烦点
// UITextField 示例
@protocol UITextFieldDelegate <NSObject>
- (BOOL)textFieldShouldBeginEditing:(UITextField *)textField;        // return NO to disallow editing.
- (void)textFieldDidBeginEditing:(UITextField *)textField;           // became first responder
- (BOOL)textFieldShouldEndEditing:(UITextField *)textField;          // return YES to allow editing to stop and to resign first responder status. NO to disallow the editing session to end
- (void)textFieldDidEndEditing:(UITextField *)textField;   
// UITableView 示例
@protocol UITableViewDelegate<NSObject, UIScrollViewDelegate>
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath;
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section;
- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section;
- (nullable UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section;   // custom view for header. will be adjusted to default or specified header height
- (nullable UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section;   // custom view for footer. will be adjusted to default or specified footer height
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • *面试心声:其实这些题本人都没怎么背,但是在上海 两周半 面了大约10家 收到差不多3个offer,总结起来就是把...
    Dove_iOS阅读 27,312评论 30 472
  • 1 Block机制 (Very Good) Block技巧与底层解析 http://www.jianshu.com...
    Kevin_Junbaozi阅读 9,478评论 3 48
  • 时间总是在不知不觉中消失, 我是不是已经把你忘记了???
    南湘雅阅读 1,111评论 0 0
  • 2015年11月27日 00:25 我亲爱的苏静 当我越来越敏感时 任何人有心无心有意无意的话都会使我自行惭秽 狠...
    阿阿阿阿阿北阅读 2,338评论 0 0
  • 如梦令 梦嗅暗香如素 回见离人无复 信手送白云 芳草长亭轻暮 无负 无负 残月鹧鸪闲诉
    如绛年阅读 1,085评论 1 2