协议代理的实现

基于我自定义的继承自UIImageView的一个CustomImageView;


在.h文件中

1,声明有这样一个协议:

@protocol CustomImageViewDelegate;


2,紧接着定义一个属性:

@property (nonatomic, weak) id<CustomImageViewDelegate>delegate;

3,在结尾处定义详细部分:

@protocol CustomImageViewDelegate<NSObject>

-(void)CustomImageView:(CustomImageView*)cImageView TapedInfo:(NSDictionary*)imageInfo;

@end


在.m文件中

1,给button或则其他空间添加事件。

2,在响应事件的方法里:


-(void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event{

if (self.delegate && [self.delegate respondsToSelector:@selector(CustomImageView:TapedInfo:)]) {

[self.delegate CustomImageView:self TapedInfo:cImageInfo];

}

}

最后在其他应用CustomImageView的类里面,在接口处申明遵守此协议即可,大功告成。

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容