ios委托

看了些委托的资料,觉得这篇比较清楚易懂!

protocol-协议,就是使用了这个协议后就要按照这个协议来办事,协议要求实现的方法就一定要实现。

delegate-委托,顾名思义就是委托别人办事,就是当一件事情发生后,自己不处理,让别人来处理。

当一个A view 里面包含了B view

b view需要修改a view界面,那么这个时候就需要用到委托了。

需要几个步骤

1、首先定一个协议

2、a view实现协议中的方法

3、b view设置一个委托变量

4、把b view的委托变量设置成a view,意思就是 ,b view委托a view办事情。

5、事件发生后,用委托变量调用a view中的协议方法

例子:

 
 
  1. B_View.h:  
  2. @protocol UIBViewDelegate <NSObject> 
  3. @optional  
  4. - (void)ontouch:(UIScrollView *)scrollView; //声明协议方法  
  5. @end  
  6. @interface BView : UIScrollView<UIScrollViewDelegate> 
  7. {  
  8. id< UIBViewDelegate > _touchdelegate; //设置委托变量  
  9. }  
  10. @property(nonatomic,assign) id< UIBViewDelegate > _touchdelegate;   
  11. @end  
  12. B_View.mm:  
  13. @synthesize _touchdelegate;  
  14. - (id)initWithFrame:(CGRect)frame {  
  15. if (self = [super initWithFrame:frame]) {  
  16. // Initialization code  
  17. _touchdelegate=nil;  
  18. }  
  19. return self;  
  20. }  
  21. - (void) touchesBegan:(NSSet*)touches withEvent:(UIEvent*)event  
  22. {  
  23. [super touchesBegan:touches withEvent:event];  
  24. if(_touchdelegate!=nil && [_touchdelegate respondsToSelector: @selector(ontouch:) ] == true)   
  25. [_touchdelegate ontouch:self]; //调用协议委托  
  26. }  
  27.  
  28. @end  
  29. A_View.h:  
  30. @interface AViewController : UIViewController < UIBViewDelegate > 
  31. {  
  32. BView *m_BView;  
  33. }  
  34. @end  
  35. A_View.mm:  
  36. - (void)viewWillAppear:(BOOL)animated  
  37. {  
  38. m_BView._touchdelegate = self; //设置委托  
  39. [self.view addSubview: m_BView];  
  40. }  
  41. - (void)ontouch:(UIScrollView *)scrollView  
  42. {  
  43.    //实现协议  

小结:详解Objective-C委托协议的内容介绍完了,希望通过本本文的学习能对你有所帮助!

转自:http://mobile.51cto.com/iphone-283416.htm

另一个例子:

1 协议:

协议,类似于Java或C#语言中的接口,它限制了实现类必须拥有哪些方法。
它是对对象行为的定义,也是对功能的规范。
示例:

1
2
3
4
5
6
7
8
9
// GoodChild.h

#import <Foundation/Foundation.h>

@protocol GoodChild <NSObject>

- ( void )filialPiety;

@end

 

1
2
3
4
5
6
7
8
// Student.h

#import <Foundation/Foundation.h>
#import "GoodChild.h"
//注意实现协议的语法。
@interface Student  : NSObject<GoodChild>

@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
// Student.m
// protocol
//
// Created by sxt on 11-10-23.
// Copyright 2011年 __MyCompanyName__. All rights reserved.
//

#import "Student.h"

@implementation Student

-  ( id )init
{
self  =  [super init ];
if  (self )  {
// Initialization code here.
}

return self;
}
- ( void )filialPiety {
NSLog ( @ "孝敬父母。。" );
}
@end

此例中定义了一个协议GoodChild,类Student实现了此协议,所以必须有filialPiety方法。
每个类虽只有一个父类,但可以实现多个协议,以逗号隔开便可。语法如下:

1
2
3
@interface Student  : NSObject<协议 1,协议 2>

@end

2 委托:

委托是objC中使用非常频繁的一种设计模式,它的实现与协议的使用是分不开的,让我们看一个综合示例:
小公司老板日常的工作是管理公司、教导新员工、发工资与接电话。
其中管理公司、教导新员工是老板要亲为的。
而发工资与接电话老板希望招聘一个秘书来帮忙,于是对秘书的要求就是要略懂出纳发工资,要能帮助领导接电话。 而这两项要求便是协议,对类功能的限定。

1
2
3
4
5
6
7
8
9
10
11
// SecProtocol.h

#import <Foundation/Foundation.h>

@protocol SecProtocol <NSObject>
//发工资
- ( void )payoff;
//接电话
- ( void )tel;

@end

然后定义一个秘书类

1
2
3
4
5
6
7
// Sec.h

#import <Foundation/Foundation.h>
#import "SecProtocol.h"
@interface Sec  : NSObject<SecProtocol>

@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
// Sec.m

#import "Sec.h"

@implementation Sec

-  ( id )init
{
self  =  [super init ];
if  (self )  {
// Initialization code here.
}

return self;
}

- ( 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
// Boss.h

#import <Foundation/Foundation.h>
#import "SecProtocol.h"
@interface Boss  :  NSObject

//此属性用于指定秘书对象,此对象必须实现SecProtocol协议。
@property (nonatomic,retain ) id<SecProtocol> detegate;
//管理
- ( 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
// Boss.m
#import "Boss.h"

@implementation Boss

@synthesize detegate =_detegate;

-  ( id )init
{
self  =  [super init ];
if  (self )  {
// Initialization code here.
}

return self;
}

- ( 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

那么老板就具有这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
// main.m
// delegate
//
// Created by sxt on 11-10-23.
// Copyright 2011年 Jinlong Wei. All rights reserved.
//

#import <Foundation/Foundation.h>
#import "Boss.h"
#import "Sec.h"
int main  ( 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 ];
return  0;
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值