How do I create delegates in Objective-C?

本文介绍了Objective-C中的委托机制,包括定义和使用自定义委托的方法。文章详细解释了非正式协议和正式协议两种方式,并提供了创建和指定委托的具体实例。

n Objective-C delegate is just an object (generally any object) that has been assigned as a delegate of another. There's no special process for creating them; you simply define a class that implements the delegate methods you're interested in. (Though with delegates that use a formal protocol, you must declare your delegate to implement that protocol; see below.)

For example, suppose you have an NSWindow. If you'd like to implement its delegate's windowDidMove:method, you could create a class like this:

@implementation MyClass
- (void)windowDidMove:(NSNotification*)notification {
   
// ...
}
@end

Then you could create an instance of MyClass and assign it as the window's delegate:

MyClass *myDelegate = [[MyClass alloc] init];
[window setDelegate: myDelegate];

On the NSWindow side, it probably has code similar to this to see if the delegate responds to the windowDidMove: message using respondsToSelector: and send it if appropriate.

if([[self delegate] respondsToSelector:@selector(windowDidMove:)]) {
   
[[self delegate] windowDidMove:notification];
}

To define your own delegates, you'll have to declare their methods somewhere. There are two basic approaches, discussed in the Apple Docs on protocols.:

1) An Informal Protocol

This can be done, as NSWindow does, in a category on NSObject. For example, continuing the example above, this is paraphrased from NSWindow.h:

@interface NSObject(NSWindowNotifications)
- (void)windowDidMove:(NSNotification *)notification;
// ... other methods here
@end

You would then use respondsToSelector:, as described above, when calling this method. Delegates simply implement this method, and they're done. This method is easy and commonly used for delegates.

2) A Formal Protocol

The other option is to declare a formal protocol. The declaration would look like this:

@protocol NSWindowNotifications
@optional
- (void)windowDidMove:(NSNotification *)notification;
// ... other methods here
@end

This is analogous to an interface or abstract base class, as it creates a special type for your delegate, NSWindowNotifications in this case. Delegate implementors would have to adopt this protocol:

@interface MyDelegate < NSWindowNotifications >
// ...
@end

And then implement the methods in the protocol. For methods declared in the protocol as @optional (like most delegate methods), you still need to check with respondsToSelector: before calling a particular method on it. Apple recommends this method, because it doesn't mess with NSObject and can provide better tool support.

 

 

 

http://jonsterling.github.com/2009/08/01/using-custom-delegates-in-objective-c.html

http://stackoverflow.com/questions/626898/how-do-i-create-delegates-in-objective-c

项目概述 这是一个基于 SSM(Spring + SpringMVC + MyBatis) 框架构建的农产品销售平台,采用传统的 JSP 作为前端视图技术。项目实现了完整的农产品电商业务流程,包括用户管理、商家管理、商品管理、购物车、订单处理、物流配送等核心功能模块。 核心功能模块 1. 用户角色体系 项目实现了三端用户体系: 角色 说明 主要功能 管理员 系统最高权限 用户管理、商家审核、商品审核、订单管理、数据统计 商家 农产品销售方 商品发布、库存管理、订单处理、发货管理 用户 消费者 商品浏览、购物车、下单购买、订单跟踪、收货确认 2. 商品管理模块(Shangpinxinxi) 功能特性: 商品信息的增删改查 商品分类管理(水果/蔬菜/粮食等) 商品图片上传 库存与销量统计 商品搜索与筛选 3. 购物车模块(Gouwuche) 功能特性: 添加商品到购物车 修改购买数量 删除购物车商品 购物车批量结算 4. 订单管理模块 订单状态流转: 5. 物流管理模块 发货模块(Fahuo): 商家发货处理 物流信息录入 签收模块(Qianshou): 用户确认收货 订单完成处理 取消订单模块(Quxiaodingdan): 用户取消订单 库存回滚处理 6. 地址管理模块(Shouhuodizhi) 用户收货地址的增删改查 默认地址设置 7. 反馈与回复模块 在线反馈(Zaixianfankui): 用户提交反馈意见 反馈回复(Fankuihuifu): 管理员回复用户反馈 8. 轮播图模块(Lunbotu) 首页轮播图管理 支持图片上传与排序
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值