咖啡动力 发表于 2013-1-26 15:48:46

objective-c协议的使用方法

一,协议的定义
@protocol test
-(void) testpocol:(int)t;
@end
 
二,协议的继承
h头文件
#import “test.h”    导入协议
@interface testViewController:UIViewController <test>{
//id<test> testp;
}
m实现文件
@implementation testViewController
-(void)viewDidLoad{
;
//调用实现方法的类
testdiaoyong  *td=[ init];
td.testd=self;   把当前实现协议的类对象赋给需要使用的地方
 
//也可以使用下面的方法传递协议

}
 
-(void) testpocol:(int)s{
 
NSLog(@"testpocol...............................................................%d",s);
}
@end
 
 
三,调用协议
 
@interface testdiaoyong  : NSObject{
id<test> testp;
}
-(void)setpoco:t;
-(void)setlen;
@end
 
 
@implementation testdiaoyong
@synthesize testp;
//协议t可以不负类型
-(void)setpoco:t{
self.testp=t;
}
-(void)start{
;
}
@end
 
页: [1]
查看完整版本: objective-c协议的使用方法