当对象A发生一些行为,想告知对象B;
这时需要将对象A的代理对象(_delegate)设置成对象B,对象B实现代理方法即协议(protocol),当对象A发生某些行为之后,调用_delegate的相应方法,即可通知对象B。
ClassADelegate
@protocol ClassADelegate
-(void)notify();
@end
ClassA
@interface ClassA : NSObject
@protocol (nonatomic,strong) id delegate;
-(void)happenSomething;
@end
@implementation ClassA
-(void)happenSomething{
[self.delegate notify];
}
@end
ClassB
@interface ClassB : NSObject
@end
@implementation ClassB
-(void)notify{
NSLog(@'ClassA happen something');
}
@end
main.m
int main(int argc,const char *argv[]){
ClassA *classa = [ClassA new];
&nbs
|