使用代理进行数据传递
1、新建类demoView 继承UIView
//定义代理协议demoDelegate .h文件中
@protocol demoDelegate<NSobject>
@optional
-(void)demoClick:(demoView *)demo;
@end
//定义代理属性
@interface demoView:UIView
@proerty(nonatomic,weak)id<demoDelegate>delegate;
@end
2、.m文件中
//要执行代理的地方写
if([self.delegate respondsToSelector:@select(demoCllick)]){ //检测代码有没有实现
[self.delegate demoClick:self];
}
//实现代理方法
3、在其他类中,以UIviewControl为例
@interface viewControl()<demoView> //准守代理协议
demoView *demo =[ [demoView alloc]init];
demo.delegate = self; //设置代理对象调用
-(void)demoClick::(demoView *)demo{
//实现代理方法
}