黑马程序员技术交流社区

标题: 在Objective-c中如何使用协议是本文要介绍的内容 [打印本页]

作者: jsh_513    时间: 2015-9-18 21:45
标题: 在Objective-c中如何使用协议是本文要介绍的内容
在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{  
[super viewDidLoad];  
//调用实现方法的类  
testdiaoyong  *td=[[testdiaoyong alloc] init];
td.testd=self;   把当前实现协议的类对象赋给需要使用的地方
//也可以使用下面的方法传递协议  
[td  setpoco: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{  
[testp testpocol:99];  
}  
@end





欢迎光临 黑马程序员技术交流社区 (http://bbs.itheima.com/) 黑马程序员IT技术论坛 X3.2