在Objective-c中如何使用協議
作者:佚名
本文介紹的是在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
小結:在Objective-c中如何使用協議的內容介紹完了,希望本文對你有所幫助。
責任編輯:zhaolei
來源:
互聯網