Objective-C學(xué)習(xí)文檔之協(xié)議使用方法
作者:佚名
Objective-C學(xué)習(xí)文檔之協(xié)議使用方法是本文要介紹的內(nèi)容,主要是來了解并學(xué)習(xí)Objective-C中協(xié)議的問題,具體內(nèi)容來看本文內(nèi)容詳解。
Objective-C學(xué)習(xí)文檔之協(xié)議使用方法是本文要介紹的內(nèi)容,主要是來了解并學(xué)習(xí)Objective-C中協(xié)議的問題,具體內(nèi)容來看本文內(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可以不負(fù)類型
- -(void)setpoco:t{
- self.testp=t;
- }
- -(void)start{
- [testp testpocol:99];
- }
- @end
小結(jié):Objective-C學(xué)習(xí)文檔之協(xié)議使用方法的內(nèi)容介紹完了,希望通過本文的學(xué)習(xí)能對你有所幫助!
責(zé)任編輯:zhaolei
來源:
網(wǎng)絡(luò)轉(zhuǎn)載