在Objective-c中如何使用协议

移动开发 iOS
本文介绍的是在Objective-c中如何使用协议,主要介绍的是协议的一些概念和操作,先来看内容。

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中如何使用协议的内容介绍完了,希望本文对你有所帮助。

责任编辑:zhaolei 来源: 互联网
相关推荐

2011-08-15 17:47:13

Objective-CisMemberOfC

2011-08-03 15:51:48

Objective-C 协议 委托

2013-04-11 13:41:30

Objective-CiOS编程

2011-08-04 17:13:48

Objective-C 字符串

2011-08-15 14:32:42

Objective-C委托协议

2011-08-22 15:31:35

Objective-C协议

2015-11-18 10:16:38

OC代码

2011-07-19 17:18:35

Objective-C Property

2011-08-02 15:55:31

Objective-C NSAutorele

2011-08-04 15:52:48

Objective-C HTML

2011-08-04 10:04:17

Objective-C 分类 协议

2012-06-25 10:14:33

WindowsGNUstepObjective-C

2011-07-25 10:30:41

Objective-C Xcode 重构

2011-07-25 11:02:29

Objective-C Xcode 标签

2011-07-25 10:14:13

Objective-C Xcode

2011-08-17 11:05:22

Objective-C方法

2011-08-10 18:07:29

Objective-C反射

2011-08-16 17:43:47

Objective-C内存管理Autorelease

2011-08-16 10:23:04

Objective-CNSAutoreleaXcode常用键

2011-08-10 17:16:01

Objective-C静态变量
点赞
收藏

51CTO技术栈公众号