協議代理的實現

基于我自定義的繼承自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的類里面,在接口處申明遵守此協議即可,大功告成。

最后編輯于
?著作權歸作者所有,轉載或內容合作請聯系作者
平臺聲明:文章內容(如有圖片或視頻亦包括在內)由作者上傳并發布,文章內容僅代表作者本人觀點,簡書系信息發布平臺,僅提供信息存儲服務。

推薦閱讀更多精彩內容