IOS开发之----协议与委托(Protocol and Delegate) 实例解析
来源:互联网 发布:计算机算法 编辑:程序博客网 时间:2024/05/18 03:10
转载自
http://blog.sina.com.cn/s/blog_71715bf80101b9od.html1 协议:
协议,类似于Java或C#语言中的接口,它限制了实现类必须拥有哪些方法。
它是对对象行为的定义,也是对功能的规范。
在写示例之前我给大家说下@required和@optional这两个关键字
他们两个是在声明协议的时候用到,@required是必须实现的方法,要不会报黄色警告.@optional是可选实现!实现还是不实现都不会报警告!
示例:
1
2
3
4
5
6
7
8
9
2
3
4
5
6
7
8
9
// GoodChild.h
#import
@protocolGoodChild
-(void)filialPiety;
@end
#import
@protocol
-(void)filialPiety;
@end
1
2
3
4
5
6
7
8
2
3
4
5
6
7
8
// Student.h
#import
#import "GoodChild.h"
//注意实现协议的语法。
@interfaceStudent : NSObject
@end
#import
#import "GoodChild.h"
//注意实现协议的语法。
@interface
@end
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
// Student.m
// protocol
//
// Created by sxt on 11-10-23.
// Copyright 2011年 __MyCompanyName__. All rights reserved.
//
#import "Student.h"
@implementationStudent
-(id)init
{
self= [super init];
if(self) {
// Initialization code here.
}
returnself;
}
-(void)filialPiety{
NSLog(@"孝敬父母。。");
}
@end
// protocol
//
// Created by sxt on 11-10-23.
// Copyright 2011年 __MyCompanyName__. All rights reserved.
//
#import "Student.h"
@implementation
-
{
self
if
// Initialization code here.
}
return
}
-(void)filialPiety{
NSLog(@"孝敬父母。。");
}
@end
此例中定义了一个协议GoodChild,类Student实现了此协议,所以必须有filialPiety方法。
每个类虽只有一个父类,但可以实现多个协议,以逗号隔开便可。语法如下:
1
2
3
2
3
@interface Student : NSObject<协议1,协议2>
@end
@end
2 委托:
委托是objC中使用非常频繁的一种设计模式,它的实现与协议的使用是分不开的,让我们看一个综合示例:
小公司老板日常的工作是管理公司、教导新员工、发工资与接电话。
其中管理公司、教导新员工是老板要亲为的。
而发工资与接电话老板希望招聘一个秘书来帮忙,于是对秘书的要求就是要略懂出纳发工资,要能帮助领导接电话。 而这两项要求便是协议,对类功能的限定。
1
2
3
4
5
6
7
8
9
10
11
2
3
4
5
6
7
8
9
10
11
// SecProtocol.h
#import
@protocolSecProtocol
//发工资
-(void)payoff;
//接电话
-(void)tel;
@end
#import
@protocol
//发工资
-(void)payoff;
//接电话
-(void)tel;
@end
然后定义一个秘书类
1
2
3
4
5
6
7
2
3
4
5
6
7
// Sec.h
#import
#import "SecProtocol.h"
@interfaceSec : NSObject
@end
#import
#import "SecProtocol.h"
@interface
@end
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
// Sec.m
#import "Sec.h"
@implementationSec
-(id)init
{
self= [super init];
if(self) {
// Initialization code here.
}
returnself;
}
-(void)payoff{
NSLog(@"sec payoff");
}
-(void)tel{
NSLog(@"sec tel");
}
@end
#import "Sec.h"
@implementation
-
{
self
if
// Initialization code here.
}
return
}
-(void)payoff{
NSLog(@"sec payoff");
}
-(void)tel{
NSLog(@"sec tel");
}
@end
紧接着是老板类:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
2
3
4
5
6
7
8
9
10
11
12
13
14
// Boss.h
#import
#import "SecProtocol.h"
@interfaceBoss : NSObject
//此属性用于指定秘书对象,此对象必须实现SecProtocol协议。
@property(nonatomic,retain)id detegate;
//管理
-(void)manage;
//教导新员工
-(void)teach;
@end
#import
#import "SecProtocol.h"
@interface
//此属性用于指定秘书对象,此对象必须实现SecProtocol协议。
@property(nonatomic,retain)
//管理
-(void)manage;
//教导新员工
-(void)teach;
@end
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
// Boss.m
#import "Boss.h"
@implementationBoss
@synthesizedetegate=_detegate;
-(id)init
{
self= [super init];
if(self) {
// Initialization code here.
}
returnself;
}
-(void)manage{
NSLog(@"boss manage");
}
-(void)teach{
NSLog(@"boss teach");
}
-(void)payoff{
NSAutoreleasePool*p=[[NSAutoreleasePool alloc] init];
[_detegate payoff];
[p release];
}
-(void)tel{
NSAutoreleasePool*p=[[NSAutoreleasePool alloc] init];
[_detegate tel];
[p release];
}
@end
#import "Boss.h"
@implementation
@synthesize
-
{
self
if
// Initialization code here.
}
return
}
-(void)manage{
NSLog(@"boss manage");
}
-(void)teach{
NSLog(@"boss teach");
}
-(void)payoff{
NSAutoreleasePool
[_detegate payoff];
[p release];
}
-(void)tel{
NSAutoreleasePool
[_detegate tel];
[p release];
}
@end
那么老板就具有这4个方法,当调用前2个时是自己完成功能,而调用后2个时则转为调用秘书的方法。
此时我们跟秘书对象就叫做代理对象,代理模式的名字由来于此。
最后调用测试下:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
// main.m
// delegate
//
// Created by sxt on 11-10-23.
// Copyright 2011年 Jinlong Wei. All rights reserved.
//
#import
#import "Boss.h"
#import "Sec.h"
intmain (int argc, const char * argv[])
{
NSAutoreleasePool* pool = [[NSAutoreleasePool alloc] init];
//实例化老板对象
Boss*boss=[[[Boss alloc] init] autorelease];
//实例化秘书对象
Sec*sec=[[[Sec alloc] init] autorelease];
//设置老板的代理对象为秘书
boss.detegate=sec;
//调用4个方法。
[boss payoff];
[boss tel];
[boss manage];
[boss teach];
[pool drain];
return0;
}
// delegate
//
// Created by sxt on 11-10-23.
// Copyright 2011年 Jinlong Wei. All rights reserved.
//
#import
#import "Boss.h"
#import "Sec.h"
int
{
NSAutoreleasePool
//实例化老板对象
Boss
//实例化秘书对象
Sec
//设置老板的代理对象为秘书
boss.detegate=sec;
//调用4个方法。
[boss payoff];
[boss tel];
[boss manage];
[boss teach];
[pool drain];
return
}
0 0
- 【iOS开发】协议与委托 (Protocol and Delegate) 实例解析
- iOS开发】协议与委托 (Protocol and Delegate) 实例解析
- 【iOS开发】协议与委托 (Protocol and Delegate) 实例解析
- IOS开发之----协议与委托(Protocol and Delegate) 实例解析
- 协议与委托(Protocol and Delegate)实例解析
- 协议与委托(Protocol and Delegate)实例解析
- 协议与委托 (Protocol and Delegate) 实例解析
- 协议与委托 (Protocol and Delegate) 实例解析
- 协议与委托 (Protocol and Delegate) 实例解析
- iOS开发 协议与委托(Protocol and Delegate)
- 【iOS开发】协议与委托 (Protocol and Delegate)
- 协议与委托(Protocol and Delegate)
- ios委托delegate与协议protocol
- 协议(protocol) & 委托(delegate)
- iOS开发之Protocol (协议) 和delegate(代理)
- 简单例子让你很好的理解:协议与委托 (Protocol and Delegate)
- IOS开发-Protocol协议及委托代理(Delegate)传值
- iOS开发-Protocol协议及委托代理(Delegate)传值
- 黑马程序员——Java异常
- 【云计算虚拟化】虚拟化学习认识总结
- circlePageIndicator结合viewpager的一个例子
- c 语言基础 printf的理解
- LeetCode解题报告--Reverse Nodes in k-Group
- IOS开发之----协议与委托(Protocol and Delegate) 实例解析
- K-Fold Cross Validation
- iOS开发技巧之code snippets(代码片段)
- python_9(获取字典值、函数参数、函数返回值、lambda的函数用法)
- Android基础学习之Socket、Http、Json网络编程
- ubantu15.04下安装配置docker
- swift2.0 UIColor Use RGB
- device eth0 does not seem to be present, delaying initialization
- Spring4.2+SpringMVC+Mybatis3.4的集成