iOS中delegate(委托)的使用

来源:互联网 发布:python中的sleep函数 编辑:程序博客网 时间:2024/06/10 08:45
委托
       委托,就是委托他人帮自己去做什么事。也就是当自己做什么事情不方便的时候,就可以建立一个委托,这样就可以委托他人帮自己去实现什么方法。
     举例:这个周末放假有空,我想买个手机,所以我有个buyIphone 方法,但是我不知道谁能买手机,所以把这个需求发布出去(比如公布在网站上),如果有卖手机的商人(也就是说他能实现buyIphone这个方法)看到,他就会接受我的委托,(在商人自己的类中实现<XXXdelegate>),那么我的委托对象就指向了这个商人..当我要买手机的时候,直接找他就行了.
例如:在Me.h中
@protocol BuyDelegate------买手机协议  -(void)buyIphone:(NSString *)iphoneType money:(NSString *)money;@end@interface Me : NSObject----我{  id<BuyDelegate> deleage;----我要找一个具备BuyDelegate行为能力的人,帮我买手机的人}  @property(assign,nonatomic)id<BuyDelegate>delegate;@end

代码中声明了一个协议 名叫BuyDelegate,在其中有一个buyIphone方法,即一个委托项。当我要购买手机的时候只需要通过delegate 调用 BuyIphone方法即可.如下:在Me.m中

-(void)willbuy--------我要叫委托人帮我买手机{  [delegate buyIphone:@"iphone 4s" money:@"4888"];}
例如:商人类实现了这一委托(用<BuyDelegate>表示实现),在Business.h中
#import <Foundation/Foundation.h>#import"My.h"@interface Business : NSObject<BuyDelegate>@end

然后在Business.m中调用 buyIphone方法

#import"Business.h"@implementation Business-(void)buyIphone:(NSString *)iphoneType money:(NSString *)money{  NSLog(@"手机有货,这个价钱卖你了,发货中!!");}@end

接下来,应该如何把我和委托人联系起来呢?

@interface MyWeekend(周末放假) : UIResponder <UIApplicationDelegate>{      Me *ken;  // 我    Business *js; // 代理商}  @end  - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions---来到电脑城门口了{    ken = [[Me alloc]init];      // 确定男主角    js = [[Business alloc]init]; // 确定商家    ken.delegate = js;           // 男主角找到商家    [ken willbuy];               // 开始买}

最后,我想分享一下在使用委托的时候的一些心得和注意事项。
心得:delegate的命名要准确,尽量看名字就知道用法。delegate和通知有的用法有些象,但是前者是单对单的,后者是单对多的情况。
注意:在dealloc要把delegate至为nil,还有就是delegate设置属性的时候要用assign,不要用retain。

通常,一个delegate的使用过程,需要经过五步:
1.     创建一个 delegate;
2.    委托者声明一个delegate;
3.    委托者调用delegate内的方法(method);
4.    被委托者设置delegate,以便被委托者调用;
5.    被委托者实现Delegate 所定义的方法。

写了一个简单的委托的试用测试:
首先创建FunctionTest类,声明委托:

FunctionTest.h@protocol FunctionTestDelegate <NSObject>      - (void)func3;      - (void)func4;    @end    @interface FunctionTest : NSObject  {      id<FunctionTestDelegate> delegate;  }  @property (nonatomic, assign)id<FunctionTestDelegate> delegate;  - (void)func1;  - (void)func2;  @end    在FunctionTest.m中:@implementation FunctionTest  @synthesize delegate;  - (void)func1  {      NSLog(@"function 1 called");      [delegate performSelector:@selector(func3)];  }    - (void)func2  {      NSLog(@"function 2 called");      [delegate performSelector:@selector(func4)];  }  @end
在入口类Appdelegate中实现委托:

在AppDelegate.h中:

#import <UIKit/UIKit.h>  #import "FunctionTest.h"  @class ViewController;  @interface AppDelegate : UIResponder <UIApplicationDelegate,FunctionTestDelegate>  {      FunctionTest *test;  }    @property (strong, nonatomic) UIWindow *window;  @property (strong, nonatomic) ViewController *viewController;  @end  
在AppDelegate.m中:
- (void)func3  {      NSLog(@"function 3 calledns");  }  - (void)func4  {      NSLog(@"function 4 calledns");  }  - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions{    test = [[FunctionTest alloc]init];      test.delegate = self;      [test func1];    [test func2];  }

0 0
原创粉丝点击