iOS 委托模式

转载

http://blog.csdn.net/crayondeng/article/details/9368461


委托Delegate是协议的一种,通过一种@protocol的方式实现,顾名思义,就是委托他人帮自己去做什么事。也就是当自己做什么事情不方便的时候,就可以建立一个委托,这样就可以委托他人帮自己去实现什么方法。


简单的总结了一下自己用到的委托的作用有两个,一个是传值,一个是传事件。
1.所谓传值经常用在B类要把自己的一个数据或者对象传给A类,让A类去展示或者处理。(这个作用在两个View视图之间传递参数的时候特别有用)(例子一)
2. 所谓传事件就是A类发生了什么事,把这件事告诉关注自己的人,也就是委托的对象,由委托的对象去考虑发生这个事件后应该做出什么反映。简单的说,假如A类 发生某个事件,它本身并不出来,而是通过委托delegate的形式,让它的委托对象B类去处理(当然委托对象B就要实现委托中的方法)。(例子二)
----下面都会有例子展示。

实现委托的过程中还要注意一些问题:

1、委托过程中需要定义协议@protocol ,这个协议可以单独New File成一个单独的协议文件,也可以放在委托对象的头文件中,一般的习惯是后者。

2、在协议中定义委托对象需要委托别人处理的一些方法,用于传值或者传事件。

3、委托类中需要定义一个协议的实例对象,注意属性一般设置为assign而非retain(一般命名为delegate,但是注意假如给类中原本就又这个属性,就要换一个名字,通过这个协议实例对象就可以调用协议中的方法(即委托方法)。

4、被委托类中需要在自身的interface中声明协议:<XXXDelegate>,表示该类要实现XXXDelegate协议中的方法。

5、注意最后要把委托类对象的delegate设置为被委托类对象,一般的处理有两种方法:

委托类对象.delegate = 被委托类对象

②在被委托类里定义一个委托类对象,并设置委托类对象.delegate = self (例子三)

下面通过三个例子demo就可以更生动的体会了。

一、通过委托传值

下面简要说明一下这个例子:

委托类是:Customer,其中委托协议中定义了一个方法,该方法表示customer要买一个iphone(会传递一个iphone型号参数),customer通过委托delegate调用这个方法表示customer要买iphone。

被委托类是:Businessman,其继承这个协议,实现了协议中的方法,也即处理了委托类customer要买iphone的需要。

下面贴代码:

Customer.h

  1. #import <Foundation/Foundation.h>  
  2.   
  3. @protocol MyDelegate <NSObject>  
  4.   
  5. -(void)buyIphone:(NSString*)iphoneType;  
  6.   
  7. @end  
  8.   
  9. @interface Customer : NSObject  
  10.   
  11. @property(nonatomic,assign)id<MyDelegate> delegate;  
  12.   
  13. -(void)willBuy;  
  14.   
  15. @end  

Customer.m

  1. #import "Customer.h"  
  2.   
  3. @implementation Customer  
  4.   
  5. @synthesize delegate;  
  6.   
  7. -(void)willBuy {  
  8.     [delegate buyIphone:@"Iphone5"];  
  9. }  
  10.   
  11. @end  

Businessman.h

  1. #import <Foundation/Foundation.h>  
  2. #import "Customer.h"  
  3.   
  4. @interface Businessman : NSObject<MyDelegate>  
  5.   
  6. @end  

Businessman.m

  1. #import "Businessman.h"  
  2.   
  3. @implementation Businessman  
  4.   
  5. -(void)buyIphone:(NSString *)iphoneType {  
  6.     NSLog(@"There is an Iphone store,we have %@",iphoneType);  
  7. }  
  8.   
  9.   
  10. @end  

main.m

  1. #import <Foundation/Foundation.h>  
  2.   
  3. #import "Customer.h"  
  4. #import "Businessman.h"  
  5.   
  6. int main(int argc, const char * argv[])  
  7. {  
  8.   
  9.     @autoreleasepool {  
  10.           
  11.         // insert code here...  
  12.         Customer *customer = [[Customer alloc]init];          
  13.         Businessman *businessman = [[Businessman alloc]init];  
  14.         customer.delegate = businessman;  
  15.         [customer willBuy];  
  16.     }  
  17.     return 0;  
  18. }  

二、通过委托传事件

下面也是简单说一下这个例子:

委托类:Boss 他要处理起草文件和接电话的任务,但是他本身并不实现这些事件响应的方法,而是通过委托让他的被委托类来实现这些响应方法。

被委托类:Secretary 他受Boss的委托实现起草文件和接电话任务的方法。

下面贴代码:

Boss.h

  1. #import <Foundation/Foundation.h>  
  2.   
  3. @protocol MissionDelegate <NSObject>  
  4.   
  5. -(void)draftDocuments;  
  6.   
  7. -(void)tellPhone;  
  8.   
  9. @end  
  10.   
  11. @interface Boss : NSObject  
  12.   
  13. @property(nonatomic, assign)id<MissionDelegate> delegate;  
  14.   
  15. -(void)manage;  
  16.   
  17. @end  

Boss.m

  1. #import "Boss.h"  
  2.   
  3. @implementation Boss  
  4.   
  5. @synthesize delegate = _delegate;  
  6.   
  7. -(void)manage {  
  8.     [_delegate draftDocuments];  
  9.     [_delegate tellPhone];  
  10. }  
  11. @end  

Secretary.h

  1. #import <Foundation/Foundation.h>  
  2. #import "Boss.h"  
  3. @interface Secretary : NSObject <MissionDelegate>  
  4.   
  5. @end  

Secretary.m

  1. #import "Secretary.h"  
  2.   
  3. @implementation Secretary  
  4.   
  5. -(void)draftDocuments {  
  6.     NSLog(@"Secretary draft documents");  
  7. }  
  8.   
  9. -(void)tellPhone {  
  10.     NSLog(@"Secretary tell phone");  
  11. }  
  12.   
  13. @end  

main.m

  1. #import <Foundation/Foundation.h>  
  2. #import "Secretary.h"  
  3. #import "Boss.h"  
  4.   
  5. int main(int argc, const char * argv[])  
  6. {  
  7.   
  8.     @autoreleasepool {  
  9.           
  10.         // insert code here...  
  11.         Boss *boss = [[Boss alloc] init];  
  12.         Secretary *secretary =  [[Secretary alloc] init];  
  13.           
  14.         boss.delegate = secretary;  
  15.         [boss manage];  
  16.     }  
  17.     return 0;  
  18. }  

三、这个例子两个view视图之间传递参数

定义一个MyView类,在这个视图中添加了一个button,button的事件响应他本身不处理,而让被委托类去处理,所以它就是委托类。

在主视图中,添加一个MyView类的实例对象,设置该实例对象的代理为self,所以它就是委托类了。

下面还是贴代码,应该都是很容易看懂的。

MyView.h

  1. #import <UIKit/UIKit.h>  
  2.   
  3. @protocol MyDelegate <NSObject>  
  4.   
  5. -(void)print:(NSString*)viewName;  
  6.   
  7. @end  
  8.   
  9. @interface MyView : UIView  
  10.   
  11. @property(nonatomic,assign)id<MyDelegate> mydelegate;  
  12.   
  13. @end  

MyView.m

  1. #import "MyView.h"  
  2.   
  3. @implementation MyView  
  4.   
  5.   
  6. @synthesize mydelegate = _mydelegate;  
  7.   
  8. - (id)initWithFrame:(CGRect)frame  
  9. {  
  10.     self = [super initWithFrame:frame];  
  11.     if (self) {  
  12.           
  13.         //代码创建一个button  
  14.         UIButton *button = [UIButton buttonWithType:UIButtonTypeRoundedRect];  
  15.         [button setTitle:@"Button" forState:UIControlStateNormal];  
  16.         [button setFrame:CGRectMake(10, 10, 100, 50)];  
  17.         [button setTintColor:[UIColor blueColor]];  
  18.           
  19.         //Target-Action模式   为button指定事件处理对象target为self,事件处理方法为buttonPressed  
  20.         [button addTarget:self action:@selector(buttonPressed) forControlEvents:UIControlEventTouchUpInside];  
  21.         [self addSubview:button];  
  22.           
  23.     }  
  24.     return self;  
  25. }  
  26. //事件处理的响应方法  
  27. -(void)buttonPressed{  
  28.       
  29.     [_mydelegate print:@"this is a view"];  
  30. }  
  31.   
  32. @end  

DelegateViewController.h

  1. #import <UIKit/UIKit.h>  
  2. #import "MyView.h"  
  3.   
  4. @interface DelegateViewController : UIViewController<MyDelegate>  
  5.   
  6. @end  

DelegateViewController.m

  1. #import "DelegateViewController.h"  
  2.   
  3. @interface DelegateViewController ()  
  4.   
  5. @end  
  6.   
  7. @implementation DelegateViewController  
  8.   
  9. - (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil  
  10. {  
  11.     self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];  
  12.     if (self) {  
  13.         // Custom initialization  
  14.     }  
  15.     return self;  
  16. }  
  17.   
  18. - (void)viewDidLoad  
  19. {  
  20.     [super viewDidLoad];  
  21.     // Do any additional setup after loading the view.  
  22.     MyView *myView = [[MyView alloc]initWithFrame:CGRectMake(50, 100, 200, 100)];  
  23.     [myView setBackgroundColor:[UIColor yellowColor]];  
  24.     myView.mydelegate = self;  
  25.     [self.view addSubview:myView];  
  26. }  
  27.   
  28. -(void)print:(NSString *)viewName {  
  29.     NSLog(@"%@",viewName);  
  30. }  
  31.   
  32. - (void)didReceiveMemoryWarning  
  33. {  
  34.     [super didReceiveMemoryWarning];  
  35.     // Dispose of any resources that can be recreated.  
  36. }  
  37.   
  38. @end  

ok,研究了一个上午,大致算是弄懂了吧!大笑
原文地址:https://www.cnblogs.com/ioschen/p/3311873.html