iOS 委托模式(2)
来源:未知 责任编辑:责任编辑 发表时间:2014-02-02 17:44 点击:次
p>
p>-(void)willBuy;
p>
p>@end
p>
p>#import <Foundation/Foundation.h>
p>
p>@protocol MyDelegate <NSObject>
p>
p>-(void)buyIphone:(NSString*)iphoneType;
p>
p>@end
p>
p>@interface Customer : NSObject
p>
p>@property(nonatomic,assign)id<MyDelegate> delegate;
p>
p>-(void)willBuy;
p>
p>@end
p>Customer.m
p>
p>
p>
p>[cpp]
p>#import "Customer.h"
p>
p>@implementation Customer
p>
p>@synthesize delegate;
p>
p>-(void)willBuy {
p> [delegate buyIphone:@"Iphone5"];
p>}
p>
p>@end
p>
p>#import "Customer.h"
p>
p>@implementation Customer
p>
p>@synthesize delegate;
p>
p>-(void)willBuy {
p> [delegate buyIphone:@"Iphone5"];
p>}
p>
p>@end
p>Businessman.h
p>
p>
p>[cpp]
p>#import <Foundation/Foundation.h>
p>#import "Customer.h"
p>
p>@interface Businessman : NSObject<MyDelegate>
p>
p>@end
p>
p>#import <Foundation/Foundation.h>
p>#import "Customer.h"
p>
p>@interface Businessman : NSObject<MyDelegate>
p>
p>@end
p>Businessman.m
p>
p>
p>[cpp]
p>#import "Businessman.h"
p>
p>@implementation Businessman
p>
p>-(void)buyIphone:(NSString *)iphoneType {
p> NSLog(@"There is an Iphone store,we have %@",iphoneType);
p>}
p>
p>
p>@end
p>
p>#import "Businessman.h"
p>
p>@implementation Businessman
p>
p>-(void)buyIphone:(NSString *)iphoneType {
p> NSLog(@"There is an Iphone store,we have %@",iphoneType);
p>}
p>
p>
p>@end
p>main.m
p>
p>
p>[cpp]
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;
相关新闻>>
- 发表评论
-
- 最新评论 更多>>