最精简 代理代码
@protocol XieYi <NSObject>
//1, 定义
- (void)star;
@end
@interface Shixian : NSObject <XieYi>
@end
@implementation Shixian
//4, 实现
- (void)star
{
NSLog(@" 这是 协议方法的 实现");
}
@end
@interface ShiYong : NSObject
//2, 设置属性
@property (nonatomic,assign) id <XieYi> delegate;
- (void)crying;
@end
@implementation ShiYong
//3 发起使用
- (void)crying
{ //3, 使用
[self.delegate star];
}
- (void)dealloc
{
self.delegate = nil;
[super dealloc];
}
@end
int main(int argc, const char * argv[]){
@autoreleasepool {
Shixian *shixian = [Shixian new];
ShiYong *shiyong = [ShiYong new];
//5,设置代理
shiyong.delegate = shixian;
//6,
[shiyong crying];
}
return 0;
}