delegate-代理设计模型-自定义

2020-07-23  本文已影响0人  js_huh

delegate-代理的简单实现-自定义


是什么?

思路


示例代码

-- WineCell类 (委托方)
-- 定义个内部协议,声明代理属性
@class Wine;
-- 内部协议
@protocol WineCellDelegate<NSObject>
-(void)plusTotal:(Wine *) wine;
-(void)minusTotal:(Wine *) wine;
@end

@interface WineCell : UITableViewCell
-- 代理属性
@property (nonatomic, weak)id<WineCellDelegate> delegate;
@end

-- 点击➕ 按钮,调用代理方法
@implementation WineCell
- (IBAction)plusClick:(id)sender {
    ....
    if([self.delegate respondsToSelector:@selector(plusTotal:)]){
        [self.delegate plusTotal:self.wine];
    }
}
-- 代理方
-- 遵循代理协议,
@interface ViewController ()<WineCellDelegate> 
@end

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
  WineCell *cell = [tableView dequeueReusableCellWithIdentifier:ID];
  cell.delegate = self; -- 设置代理对象
  return cell;
}
-- 实现代理方法
-(void)plusTotal:(Wine *) wine { ........ }

注意:


也可以看看


来自于哪里?

上一篇下一篇

猜你喜欢

热点阅读