Objective-C學習文檔之協(xié)議使用方法
作者:佚名
Objective-C學習文檔之協(xié)議使用方法是本文要介紹的內容,主要是來了解并學習Objective-C中協(xié)議的問題,具體內容來看本文內容詳解。
Objective-C學習文檔之協(xié)議使用方法是本文要介紹的內容,主要是來了解并學習Objective-C中協(xié)議的問題,具體內容來看本文內容詳解。
一、協(xié)議的定義
- @protocol test
- -(void) testpocol:(int)t;
- @end
二、協(xié)議的繼承
h頭文件
- #import “test.h” //導入協(xié)議
- @interface testViewController:UIViewController <test>{
- //id<test> testp;
- }
m實現文件
- @implementation testViewController
- -(void)viewDidLoad{
- [super viewDidLoad];
- //調用實現方法的類
- testdiaoyong *td=[[testdiaoyong alloc] init];
- td.testd=self; 把當前實現協(xié)議的類對象賦給需要使用的地方
- //也可以使用下面的方法傳遞協(xié)議
- [td setpoco:self]
- }
- -(void) testpocol:(int)s{
- NSLog(@"testpocol.........%d",s);
- }
- @end
三、調用協(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
小結:Objective-C學習文檔之協(xié)議使用方法的內容介紹完了,希望通過本文的學習能對你有所幫助!
責任編輯:zhaolei
來源:
網絡轉載