object-c:委托(delegate)
来源:互联网 发布:sql select表 编辑:程序博客网 时间:2024/05/17 14:28
在.NET中都知道委托(delegate),通俗点的解释就是可以将方法作为一个参数传到另外一个方法中使用。
委托是一种引用方法的类型。一旦为委托分配了方法,委托将与该方法具有完全相同的行为。委托方法的使用可以像其他任何方法一样,具有参数和返回值。在Object C中也同样存在委托的概念,但是委托一般与协议(Protocol)同时使用,而且和.NET中的委托稍有区别
一. 定义委托(delegate)先定义一个协议(Protocol)
回顾一下上一篇所学的,先定义一个协议(Protocol) ProtocolName, 协议ProtocolName中定义了两个方法: first ,second
具体的代码如下:
#import <Foundation/Foundation.h>@protocol ProtocolName <NSObject>-(void) first;-(void) second;@end
上面定义的协议包含了两个方法,返回值类型为特殊的类型void 。 void 在Object C中也被认为是一种特殊的返回值类型。
二. 定义两个类遵循协议并实现方法
现在这里定义两个类StudentA和StudentB来实现协议ProtocolName中的方法.
StudentA 实现的代码如下:
#import <Foundation/Foundation.h>#import "ProtocolName.h"@interface StudentA : NSObject<ProtocolName>@end-----------------------------------------------------#import "StudentA.h"@implementation StudentA-(void) first{ NSLog(@"StudentA---first");}-(void) second{ NSLog(@"StudentA--second");}@end
StudentB实现代码如下:
#import <Foundation/Foundation.h>#import "ProtocolName.h"@interface StudentB : NSObject <ProtocolName>@end------------------------------------------------------------#import "StudentB.h"@implementation StudentB-(void) first{ NSLog(@"StudentB--first");}-(void) second{ NSLog(@"StudentB--second");}@end
我们注意到协议ProtocolName中的两个方法并没有使用@required 或者@optional 来修饰, 前面也提到过了,如果没有使用任何修饰那么默认是@required,那么StudentA和StudentB必须实现这两个方法,否则在编译的时候就报错。
三. 定义一个类用于代理
在上面定义了一个协议ProtocolName,然后新建一个Object C类Student,但是这个类并没有去遵循协议ProtocolName 去实现其他的方法,而是通过委托来代理实现协议方法的调用。定义委托和定义属性一样,Student中的代理属性代码实现如下:
#import <Foundation/Foundation.h>#import "ProtocolName.h"@interface Student : NSObject{ id<ProtocolName> delegate;}@property (retain) id<ProtocolName> delegate;-(void) setMethod;@end---------------------------------------------#import "Student.h"@implementation Student@synthesize delegate;-(void) setMethod{ NSLog(@"Student---setMethod"); [delegate first]; [delegate second];}@end
在Student.h中定义了 id<ProtocolName> delegate; 这个其实就是类中的属性,后来使用@property来修饰说明其自动实现了get set方法。这里不多说。关键看如何使用:
先创建一个StudentA的实例,同时创建一个Student的实力,将StudentA的实力赋值给delegate,这里相当于delegate代理了StudentA的实例。
Student *stu=[[Student alloc] init]; StudentA *stua=[[StudentA alloc] init]; StudentB *stub=[[StudentB alloc] init]; stu.delegate=stua; [stu.delegate first]; [stu setMethod]; stu.delegate=stub; [stu setMethod];
看到上面的代码stu.delegate=stua, 这个就是委托代理。当stu调用方法setMethod方法时候,使用委托调用了方法first和方法second。
而代码stu.delegate=stub, delegate又代理stub累,可以调用其中的方法first,second .
上面的测试结果如下:
2014-03-21 22:45:07.870 DelegateOS[577:303] StudentA---first2014-03-21 22:45:07.872 DelegateOS[577:303] Student---setMethod2014-03-21 22:45:07.872 DelegateOS[577:303] StudentA---first2014-03-21 22:45:07.872 DelegateOS[577:303] StudentA--second2014-03-21 22:45:07.873 DelegateOS[577:303] Student---setMethod2014-03-21 22:45:07.873 DelegateOS[577:303] StudentB--first2014-03-21 22:45:07.874 DelegateOS[577:303] StudentB--second
- object-c:委托(delegate)
- 【C#】 委托(Delegate)
- Object C学习笔记16-委托(delegate)
- Object C学习笔记16-委托(delegate)
- Delegate 委托 C#(ZZ)
- C#:委托(delegate)和事件 (event)
- C#-委托(delegate)
- C#-委托(delegate)
- [C#] 委托delegate
- C#:delegate委托
- Control.Invoke 方法 (Delegate, Object[]) ,执行委托
- 天天学C#-委托(delegate)
- 天天学C#-委托(delegate)
- 天天学C#-委托(delegate)
- cSharp(c#)中的delegate(委托)
- 委托(Delegate)
- 委托(Delegate)
- 委托(Delegate)
- 非控件ajax举例
- eclipse 设置 jdk 1.8 与 android sdk的问题
- geohash算法
- Mahout推荐算法API详解
- 使用TCP/IP协议实现聊天程序
- object-c:委托(delegate)
- Kafka学习3_Kafka命令行常用命令说明
- eclipse自动提示功能没了的解决办法
- vs2010中启用jquery的智能感知
- istView.setItemsCanFocus(true);
- java JDK 动态代理(如何使用,以及工作原理解析)
- Java Io 笔记总结
- java 读文件的几种方式
- Itext生成PDF文件