Object C学习笔记16-委托(delegate)
来源:互联网 发布:忘记windows登陆密码 编辑:程序博客网 时间:2024/05/17 15:02
转自:http://www.cnblogs.com/qingyuan/p/3616870.html
Object C学习笔记16-委托(delegate)
2014-03-22 07:27 by 贺臣, 3961 阅读, 0 评论, 收藏, 编辑在.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学习笔记16-委托(delegate)
- Object C学习笔记16-委托(delegate)
- object-c:委托(delegate)
- C#学习笔记-delegate 委托
- 【初学】Objective-C学习笔记-委托代理(delegate)和协议(protocol)
- C#-委托(delegate)
- C#-委托(delegate)
- [C#] 委托delegate
- C#:delegate委托
- 【C#】 委托(Delegate)
- C#“委托”学习笔记
- C#学习--委托 Delegate
- Object-C学习(六):委托模式
- C#学习笔记之——委托(delegate)
- Object-C学习笔记
- object-c学习笔记
- Object-C学习笔记
- Object-c学习笔记
- Linux学习之CentOS--虚拟机下的CentOS如何上网
- GCJ 2009 Bribe the Prisoners
- Linux 下I/O多路复用总结
- iOS项目中安装和使用 Cocoapods
- 【笔记】 《js权威指南》- 第15章 脚本化文档 - 15.8 元素的几何形状和滚动
- Object C学习笔记16-委托(delegate)
- 7.1 virtual模块的函数调用关系
- IOS-事务在数据库中的用处
- Linux下线程的调度策略与优先级
- php函数
- OpenCV中利用cvConvertScale()对图像数据作线性变换~
- js,ajax验证编号是否存在,input只能输入数字,form表单验证
- iOS 模态窗口居中弹出背景半透明
- jsessionid 如何去掉