@protocol TestDelegate
-(void) logStr:(NSString *) str;
///这里写一些方法接口
@end
使用代理
@interface XMRotationChartView : UIView
///类的接口定义代理的属性,给外界赋值
@property (nonatomic, assign) id delegate;
@end
@implementation XMRotationChartView
-(void) test{
[self.delegate logStr:@"Hello"];
}
使用代理方法的例子
///监听代理
[self addObserver:self forKeyPath:@"delegate" options:NSKeyValueObservingOptionNew context:nil];
- (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary *)change context:(void *)context{
///代理成功
if ([@"delegate"isEqualToString:keyPath]) {
if (_delegate != nil) {
NSlog(@"代理成功");
[self.delegate logStr:@"Hello"];
}else{
NSlog(@"代理被取消了");
}
}
}