在Objective-c中如何使用協(xié)議
作者:佚名
本文介紹的是在Objective-c中如何使用協(xié)議,主要介紹的是協(xié)議的一些概念和操作,先來看內(nèi)容。
在Objective-c中如何使用協(xié)議是本文要介紹的內(nèi)容,主要介紹的是協(xié)議的一些概念和操作,通過本文介紹,讓我們?nèi)チ私鈪f(xié)議,先來看內(nèi)容詳解。
一、協(xié)議的定義
- @protocol test
- -(void) testpocol:(int)t;
- @end
二、協(xié)議的繼承
h頭文件
- #import “test.h” 導(dǎo)入?yún)f(xié)議
- @interface testViewController:UIViewController <test>{
- //id<test> testp;
- }
m實現(xiàn)文件
- @implementation testViewController
- -(void)viewDidLoad{
- [super viewDidLoad];
- //調(diào)用實現(xiàn)方法的類
- testdiaoyong *td=[[testdiaoyong alloc] init];
td.testd=self; 把當(dāng)前實現(xiàn)協(xié)議的類對象賦給需要使用的地方
- //也可以使用下面的方法傳遞協(xié)議
- [td setpoco:self]
- }
- -(void) testpocol:(int)s{
- NSLog(@"testpocol....................%d",s);
- }
- @end
- 三,調(diào)用協(xié)議
- @interface testdiaoyong : NSObject{
- id<test> testp;
- }
- -(void)setpoco:t;
- -(void)setlen;
- @end
- @implementation testdiaoyong
- @synthesize testp;
- //協(xié)議t可以不負類型
- -(void)setpoco:t{
- self.testp=t;
- }
- -(void)start{
- [testp testpocol:99];
- }
- @end
小結(jié):在Objective-c中如何使用協(xié)議的內(nèi)容介紹完了,希望本文對你有所幫助。
責(zé)任編輯:zhaolei
來源:
互聯(lián)網(wǎng)