在Objective-c中如何使用協議

來源:互聯網
上載者:User

Objective-c中如何使用協議是本文要介紹的內容,主要介紹的是協議的一些概念和操作,通過本文介紹,讓我們去瞭解協議,先來看內容詳解。

一、協議的定義

 
  1. @protocol test  
  2.  
  3. -void) testpocol:int)t;  
  4. @end 

二、協議的繼承

h標頭檔

 
  1. #import “test.h”    匯入協議  
  2. @interface testViewController:UIViewController <test>{  
  3. //id<test> testp;  

m實現檔案

 
  1. @implementation testViewController  
  2.  
  3. -void)viewDidLoad{  
  4. [super viewDidLoad];  
  5. //調用實現方法的類  
  6. testdiaoyong  *td=[[testdiaoyong alloc] init]; 

td.testd=self;   把當前實現協議的類對象賦給需要使用的地方

 
  1. //也可以使用下面的方法傳遞通訊協定  
  2. [td  setpoco:self]  
  3. }  
  4.  
  5. -(void) testpocol:(int)s{  
  6. NSLog(@"testpocol....................%d",s);  
  7. }  
  8. @end  
  9.  
  10. 三,調用協議  
  11.  
  12. @interface testdiaoyong  : NSObject{  
  13. id<test> testp;  
  14. }  
  15.  
  16. -(void)setpoco:t;  
  17.  
  18. -(void)setlen;  
  19. @end  
  20.  
  21. @implementation testdiaoyong  
  22. @synthesize testp;  
  23.  
  24. //協議t可以不負類型  
  25.  
  26. -(void)setpoco:t{  
  27. self.testp=t;  
  28. }  
  29.  
  30. -(void)start{  
  31. [testp testpocol:99];  
  32. }  
  33. @end 

小結:在Objective-c中如何使用協議的內容介紹完了,希望本文對你有所協助。

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.