iphone开发中的delegate
2014-04-30 15:06
302 查看
我太忙了,雇用了一位助手,并安排了一定任务A,任务B,任务C,.. 给他。 一 接到活,属于任务A,B,C...之类的活,我自己不做,直接抛给助手去完成。
那么,“我”就是A Object. “助手”就是"我"的“Delegate”。写成代码就是: 我.delegate= 我的助手
我的助手是可以实现我给他的任务A,B,C,..的,这些任务就是协议 protocol 。由我助手来实现协议所声明的任务。
协议 Protocol :(类似java的接口, C++的虚基类)
我说下我的理解。object-c 里没有多继承。那么又要避免做出一个对象什么都会(super class monster,huge ,super,waste)一个超能对象本身是否定了面向对象的概念和真谛了。为了让代码更简洁,条理更清楚,可以将部分职责分离。
协议本身没有具体的实现。只规定了一些可以被其它类实现的接口
#import <Foundation/Foundation.h>
@protocol MyProtocol <NSObject>
@required
-(void)testProFun;
@optional
-(void)testProFun2;
@end
delegate 总是被定义为 assign @property
#import <Foundation/Foundation.h>
#import "MyProtocol.h"
@interface MyProtocolRef : NSObject
{
id<MyProtocol> _delegate;
}
@property(nonatomic,assign)id<MyProtocol>delegate;
@end
#import "MyProtocolRef.h"
@implementation MyProtocolRef
@synthesize delegate=_delegate;
-(id)init
{
self =[super init];
if (self) {
}
return self;
}
-(void)callDelegate
{
if ([self.delegate respondsToSelector:@selector(testProFun)])
{
[self.delegate testProFun];
}
if ([self.delegate respondsToSelector:@selector(testProFun2)])
{
[self.delegate testProFun2];
}
}
@end
这样我们就在MyProtocolRef 内部声明一个委托(delegate),那么就需要委托的代理实现MyProtocol 中约定的行为
// 首先, 在接口里边声明要使用谁的Delegate
#import <Foundation/Foundation.h>
#import "MyProtocol.h"
@class MyProtocolRef;
@interface MyProtocolImp : NSObject<MyProtocol>
{
MyProtocolRef *_ref;
}
@end
// 然后在实现文件中初始化的时候, 设置Delegate为self(自己) ,
//并实现MyProtocol中约定的行为
#import "MyProtocolImp.h"
#import "MyProtocolRef.h"
@implementation MyProtocolImp
-(id)init
{
self=[super init];
if (self) {
_ref=[[MyProtocolRef alloc]init];
_ref.delegate=self;
}
return self;
}
-(void)dealloc
{
[_ref release];
_ref=nil;
[super dealloc];
}
-(void)callRef
{
if (_ref) {
[_ref callDelegate];
}
}
//imp MyProtocol testProFun
-(void)testProFun
{
NSLog(@"call testProFun");
}
//imp MyProtocol testProFun2
-(void)testProFun2
{
NSLog(@"call testProFun2");
}
@end
那么,“我”就是A Object. “助手”就是"我"的“Delegate”。写成代码就是: 我.delegate= 我的助手
我的助手是可以实现我给他的任务A,B,C,..的,这些任务就是协议 protocol 。由我助手来实现协议所声明的任务。
协议 Protocol :(类似java的接口, C++的虚基类)
我说下我的理解。object-c 里没有多继承。那么又要避免做出一个对象什么都会(super class monster,huge ,super,waste)一个超能对象本身是否定了面向对象的概念和真谛了。为了让代码更简洁,条理更清楚,可以将部分职责分离。
协议本身没有具体的实现。只规定了一些可以被其它类实现的接口
#import <Foundation/Foundation.h>
@protocol MyProtocol <NSObject>
@required
-(void)testProFun;
@optional
-(void)testProFun2;
@end
delegate 总是被定义为 assign @property
#import <Foundation/Foundation.h>
#import "MyProtocol.h"
@interface MyProtocolRef : NSObject
{
id<MyProtocol> _delegate;
}
@property(nonatomic,assign)id<MyProtocol>delegate;
@end
#import "MyProtocolRef.h"
@implementation MyProtocolRef
@synthesize delegate=_delegate;
-(id)init
{
self =[super init];
if (self) {
}
return self;
}
-(void)callDelegate
{
if ([self.delegate respondsToSelector:@selector(testProFun)])
{
[self.delegate testProFun];
}
if ([self.delegate respondsToSelector:@selector(testProFun2)])
{
[self.delegate testProFun2];
}
}
@end
这样我们就在MyProtocolRef 内部声明一个委托(delegate),那么就需要委托的代理实现MyProtocol 中约定的行为
// 首先, 在接口里边声明要使用谁的Delegate
#import <Foundation/Foundation.h>
#import "MyProtocol.h"
@class MyProtocolRef;
@interface MyProtocolImp : NSObject<MyProtocol>
{
MyProtocolRef *_ref;
}
@end
// 然后在实现文件中初始化的时候, 设置Delegate为self(自己) ,
//并实现MyProtocol中约定的行为
#import "MyProtocolImp.h"
#import "MyProtocolRef.h"
@implementation MyProtocolImp
-(id)init
{
self=[super init];
if (self) {
_ref=[[MyProtocolRef alloc]init];
_ref.delegate=self;
}
return self;
}
-(void)dealloc
{
[_ref release];
_ref=nil;
[super dealloc];
}
-(void)callRef
{
if (_ref) {
[_ref callDelegate];
}
}
//imp MyProtocol testProFun
-(void)testProFun
{
NSLog(@"call testProFun");
}
//imp MyProtocol testProFun2
-(void)testProFun2
{
NSLog(@"call testProFun2");
}
@end
相关文章推荐
- 在eclipse中查看class源码
- win2003超过最大连接数
- js跨域post请求
- 禁用右键 回车 ESC 和 ALT+F4组合建
- 华为2014编程大赛初赛
- 下面就是开始自己制作第一个模块
- Python启程
- Java按行读取txt文件
- 初学Java,jar命令详解(二十四)
- RUBY的类封装,继承,多态简单演示
- 随心所欲导出你的 UI 界面到 PDF 文件
- 编译android-4.3.1_r源码并刷到自己的Galaxy Nexus I9250真机上
- ABAP--如何在'REUSE_ALV_GRID_DISPLAY'使用自定义F4帮助,返回多个字段以及计算修改其他字段
- 第9章 JSTL标签库
- hive升级0.13(0.11.0 -> 0.13.0)
- java读取txt文件
- 使用SSH协议连接GitHub(对于一个版本库从https迁移到SSH)
- UITableView.separatorInset
- 如我所想:网络何以安全
- actionbar 导航