协议用法

来源:互联网 发布:淘宝客新建定向计划 编辑:程序博客网 时间:2024/06/18 03:40
协议就是 让别的类 干一些  自己类干不了的事 , 比如 cell  不能push VC 那么就让VC 去干




1. 在cell 里面 声明 协议方法  并设置成属性   记得在 interface 上面   所以必须加上  calss

@class DiscoverCellHeadView;@protocol CollectBtnDelgate <NSObject>/** *  收藏点击 代理 * *  @param view <#view description#> */-(void)collectBtnClick:(DiscoverCellHeadView *)view WithRquest:(BOOL)request WithResultDescr:(NSString *)resultDescr;@end

2.设置成属性
@interface DiscoverCellHeadView : UIView<DLImageViewDelegate>@property (nonatomic, weak) id <CollectBtnDelgate> delgatecCollectBtn;  //代理


比如在一个cell 点击button 的时候调用   记得 传一下3个参数  需要几个  自己修改

if ([self.delgatecCollectBtn conformsToProtocol:@protocol(CollectBtnDelgate)] && [_delgatecCollectBtn respondsToSelector:@selector(collectBtnClick:WithRquest:WithResultDescr:)])    {        [self.delgatecCollectBtn collectBtnClick:self WithRquest:YES WithResultDescr:nil];    }



3.回去 VC 调用   声明  协议
@interface DiscoverVC : DLBaseAMTVC <CollectBtnDelgate>

4.设置代理

DiscoverJokeCell *cell = (DiscoverJokeCell *)[tableView dequeueReusableCellWithIdentifier:cellJokeIdentify];            if (!cell) {                cell = [[[NSBundle mainBundle] loadNibNamed:@"DiscoverJokeCell" owner:self options:nil] objectAtIndex:0];            }            cell.headView.delgatecCollectBtn = self;


5.代理方法

#pragma mark --headView 代理方法-(void)collectBtnClick:(DiscoverCellHeadView *)view WithRquest:(BOOL)request WithResultDescr:(NSString *)resultDescr{    ContentResponseModel *model = [_dataArray objectAtIndex:view.model.info.cellRow];    model.info.isCollect = view.model.info.isCollect;        if (request) {        if (model.info.isCollect == 1) {            [self presentMessageTips:@"添加收藏成功~"];        }        else        {            [self presentMessageTips:@"取消收藏成功~"];        }    }    else    {        [self presentMessageTips:resultDescr];    }}






0 0
原创粉丝点击