Objective-C学习文档之协议使用方法

移动开发 iOS
Objective-C学习文档之协议使用方法是本文要介绍的内容,主要是来了解并学习Objective-C中协议的问题,具体内容来看本文内容详解。

Objective-C学习文档之协议使用方法是本文要介绍的内容,主要是来了解并学习Objective-C中协议的问题,具体内容来看本文内容详解。

一、协议的定义

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

二、协议的继承

h头文件

#import “test.h”    //导入协议  
@interface testViewController:UIViewController <test>{  
//id<test> testp;  

  • 1.
  • 2.
  • 3.
  • 4.

m实现文件

@implementation testViewController  
-(void)viewDidLoad{  
[super viewDidLoad];  
//调用实现方法的类  
testdiaoyong  *td=[[testdiaoyong alloc] init];  
td.testd=self;   把当前实现协议的类对象赋给需要使用的地方  
//也可以使用下面的方法传递协议  
[td  setpoco:self]  
}  
-(void) testpocol:(int)s{  
 
NSLog(@"testpocol.........%d",s);  
}  
@end 
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.
  • 9.
  • 10.
  • 11.
  • 12.
  • 13.
  • 14.

三、调用协议

@interface testdiaoyong  : NSObject{  
id<test> testp;  
}  
-(void)setpoco:t;  
-(void)setlen;  
@end  
@implementation testdiaoyong  
@synthesize testp;  
//协议t可以不负类型  
-(void)setpoco:t{  
self.testp=t;  
}  
-(void)start{  
[testp testpocol:99];  
}  
 
@end 
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.
  • 9.
  • 10.
  • 11.
  • 12.
  • 13.
  • 14.
  • 15.
  • 16.
  • 17.

小结:Objective-C学习文档之协议使用方法的内容介绍完了,希望通过本文的学习能对你有所帮助!

责任编辑:zhaolei 来源: 网络转载
相关推荐

2011-08-15 17:06:01

Objective-CNSLog

2011-08-04 16:46:03

Objective-C 声明

2011-08-12 10:27:52

Objective-C对象

2011-07-19 17:18:35

Objective-C Property

2011-08-02 15:55:31

Objective-C NSAutorele

2011-08-10 17:16:01

Objective-C静态变量

2011-07-27 16:18:42

Objective-c 协议

2011-08-10 11:08:32

Objective-C字符串NSString

2013-08-21 13:26:43

Objective-CNSDate说明

2011-08-17 11:05:22

Objective-C方法

2011-07-25 10:14:13

Objective-C Xcode

2011-07-25 10:30:41

Objective-C Xcode 重构

2011-07-25 11:02:29

Objective-C Xcode 标签

2011-08-16 10:23:04

Objective-CNSAutoreleaXcode常用键

2011-08-17 12:03:36

Objective-C

2011-08-03 15:51:48

Objective-C 协议 委托

2013-04-11 13:41:30

Objective-CiOS编程

2011-07-28 15:11:23

iOS Objective-

2011-08-04 14:58:37

Objective-C Cocoa NSString

2013-08-21 14:57:42

objective-c问题
点赞
收藏

51CTO技术栈公众号