标签:
创建一个买票协议.该协议规定输出是否还有剩票
1 #import <Foundation/Foundation.h> 2 3 @protocol BuyticketsDelegate <NSObject> 4 5 @required 6 - (void) check; 7 8 @end
创建一个代理Agent遵守协议
1 #import "Buytickets.h" 2 @interface Agents : NSObject<BuyticketsDelegate> 3 4 @end 5 6 7 @implementation Agents 8 9 - (void)check{ 10 NSLog(@"还有剩票"); 11 } 12 13 @end
创建一个Person类,包含一个遵守协议的代理对象
1 @interface Person : NSObject 2 3 @property(nonatomic, assign)id<BuyticketsDelegate>delegate; 4 5 - (void)buy; 6 7 @end 8 9 10 #import "Person.h" 11 @implementation Person 12 13 - (void)buy{ 14 [_delegate check]; 15 } 16 17 @end
在main函数里,指定一个代理
1 int main(int argc, const char * argv[]) { 2 @autoreleasepool { 3 4 Person *person = [[Person alloc]init]; 5 Agents *age = [[Agents alloc]init]; 6 person.delegate = age; 7 [person buy]; 8 9 10 } 11 return 0; 12 }
标签:
原文地址:http://www.cnblogs.com/wan-huan/p/5727585.html