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
复制代码
0 0
原创粉丝点击