Objective-C学习文档之协议使用方法是本文要介绍的内容,主要是来了解并学习Objective-C中协议的问题,具体内容来看本文内容详解。
一、协议的定义
@protocol test
-(void) testpocol:(int)t;
@end
- 1.
- 2.
- 3.
二、协议的继承
h头文件
#import “test.h” //导入协议
@interface testViewController:UIViewController <test>{
//id<test> testp;
}
- 1.
- 2.
- 3.
- 4.
m实现文件
@implementation testViewController
-(void)viewDidLoad{
[super viewDidLoad];
//调用实现方法的类
testdiaoyong *td=[[testdiaoyong alloc] init];
td.testd=self; 把当前实现协议的类对象赋给需要使用的地方
//也可以使用下面的方法传递协议
[td setpoco:self]
}
-(void) testpocol:(int)s{
NSLog(@"testpocol.........%d",s);
}
@end
- 1.
- 2.
- 3.
- 4.
- 5.
- 6.
- 7.
- 8.
- 9.
- 10.
- 11.
- 12.
- 13.
- 14.
三、调用协议
@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{
[testp testpocol:99];
}
@end
- 1.
- 2.
- 3.
- 4.
- 5.
- 6.
- 7.
- 8.
- 9.
- 10.
- 11.
- 12.
- 13.
- 14.
- 15.
- 16.
- 17.
小结:Objective-C学习文档之协议使用方法的内容介绍完了,希望通过本文的学习能对你有所帮助!