iOS 委托模式(3)
来源:未知 责任编辑:责任编辑 发表时间:2014-02-02 17:44 点击:次
p> [customer willBuy];
p> }
p> return 0;
p>}
p>
p>#import <Foundation/Foundation.h>
p>
p>#import "Customer.h"
p>#import "Businessman.h"
p>
p>int main(int argc, const char * argv[])
p>{
p>
p> @autoreleasepool {
p>
p> // insert code here...
p> Customer *customer = [[Customer alloc]init];
p> Businessman *businessman = [[Businessman alloc]init];
p> customer.delegate = businessman;
p> [customer willBuy];
p> }
p> return 0;
p>}
p>二、通过委托传事件
p>
p>下面也是简单说一下这个例子:
p>
p>委托类:Boss
p> 他要处理起草文件和接电话的任务,但是他本身并不实现这些事件响应的方法,而是通过委托让他的被委托类来实现这些响应方法。
p>
p>被委托类:Secretary
p> 他受Boss的委托实现起草文件和接电话任务的方法。
p>
p>下面贴代码:
p>
p>Boss.h
p>
p>
p>
p>[cpp]
p>#import <Foundation/Foundation.h>
p>
p>@protocol MissionDelegate <NSObject>
p>
p>-(void)draftDocuments;
p>
p>-(void)tellPhone;
p>
p>@end
p>
p>@interface Boss : NSObject
p>
p>@property(nonatomic, assign)id<MissionDelegate> delegate;
p>
p>-(void)manage;
p>
p>@end
p>
p>#import <Foundation/Foundation.h>
p>
p>@protocol MissionDelegate <NSObject>
p>
p>-(void)draftDocuments;
p>
p>-(void)tellPhone;
p>
p>@end
p>
p>@interface Boss : NSObject
p>
p>@property(nonatomic, assign)id<MissionDelegate> delegate;
p>
p>-(void)manage;
p>
p>@end
p>Boss.m
p>
p>
p>
p>[cpp]
p>#import "Boss.h"
p>
p>@implementation Boss
p>
p>@synthesize delegate = _delegate;
p>
p>-(void)manage {
p> [_delegate draftDocuments];
p> [_delegate tellPhone];
p>}
p>@end
p>
p>#import "Boss.h"
p>
p>@implementation Boss
p>
p>@synthesize delegate = _delegate;
p>
p>-(void)manage {
p> [_delegate draftDocuments];
p> [_delegate tellPhone];
p>}
p>@end
p>Secretary.h
p>
p>
p>[cpp]
p>#import <Foundation/Foundation.h>
相关新闻>>
- 发表评论
-
- 最新评论 更多>>