代理通常是使用在与其他view通信
//VCThirdController.h
//创建代理类
@protocol VCThirdDelegate<NSObject>
@required
//定义代理通信方法
- (void)changeColor:(UIColor*)color;
@end
@interface VCThird : UIViewController<UIPickerViewDelegate,UIPickerViewDataSource>
//定义代理对象属性
@property (nonatomic,retain) id<VCThirdDelegate> delegate;
@end
//.h实现代理类
#import "VCThird.h"
@interface ViewController : UIViewController<VCThirdDelegate>
@end
//.m实现代理类方法
- (void)changeColor:(UIColor *)color{
self.view.backgroundColor = color;
}
//给需要通信对象的代理对象属性指向实现代理的对象
VCthird.delegate = self;
//VCThirdController.m
//此时可以进行通信
- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event{
[_delegate changeColor:[UIColor greenColor]];
}
网友评论