IOS委托详细说明

来源:互联网 发布:易语言断网瞬移源码 编辑:程序博客网 时间:2024/06/01 21:53

每次遇到IOS委托总是说的模棱两可,今天有时间所幸整理下,也借鉴网上的文章来说明:

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

委托注意事项:

1.委托中需要定义协议@protocol,这个协议可以单独定义成一个文件,但是我们习惯把委托对象放在头文件当中。

2.委托目的两个传值和事件(传值传事件个人认为没什么大区别,传值只不过是传事件方法后面加各参数罢了)

3.委托中定义协议的实例对象,注意属性一般设置为assign而非retain(一般命名为delegate,但假如类中原本有这个属性,换一个名字)

4.被委托类中需要在自身interface中声明协议:<XXXXDelegate>,表示该类要实现XXXDelegate协议中方法(有时不声明页不会出现问题,但是被委托对象委托方法按住option按键跳转无法跳转到协议声明方法当中)

5.委托对象的delegate设置为被委托类对象,两种方法一是被委托对象,一是self本身,写的程序很多都是self。


一、委托传值

Customer.h文件

#import <Foundation/Foundation.h>

@protocol MyDelegate<NSObject>

-(void)buyIphone:(NSString *)iphoneType;

@end

@interface Customer:NSObject

@property(nonatomic,assign)id<MyDelegate>delegate;

-(void)willBuy;

@end

Customer.m文件

#import "Customer.h"

@implementation Customer

@synthesize delegate;

-(void)willBuy{

[delegate buyIphone:@"Iphone5"];

}

@end

Businessman.h

#import <Foundation/Foundation.h>

#import "Customer.h"

@interface Businessman :NSObject<MyDelegate>

@end

Businessman.m文件

#import "Businessman.h"

@implementation Businessman

-(void)buyIphone:(NSString*)iphone{

NSLog(@"There is an iphone store,we have %@",iphoneTyoe);

}

@end

main.m

#import <Foundation/Foundation.h>

#import "Customer.h"

#import "Businessman.h"

int main(int argc,const char *argv[])

{

@autoreleasepool{

Customer *customer = [[Customer alloc]init];

Businessman *businessman = [[Businessman alloc]init];

customer.delegate = businessman;

        [customer willBuy];

return 0;


}

}

二、通过委托传事件 
 
下面也是简单说一下这个例子:
 
委托类:Boss
 他要处理起草文件和接电话的任务,但是他本身并不实现这些事件响应的方法,而是通过委托让他的被委托类来实现这些响应方法。
 
被委托类:Secretary
 他受Boss的委托实现起草文件和接电话任务的方法。
 
下面贴代码:
 
Boss.h

#import <Foundation/Foundation.h>   
  
@protocol MissionDelegate <NSObject>  
  
-(void)draftDocuments;  
  
-(void)tellPhone;  
  
@end  
  
@interface Boss : NSObject  
  
@property(nonatomic, assign)id<MissionDelegate> delegate;  
  
-(void)manage;  
  
@end  
Boss.m
 
 
 
[cpp] 
#import "Boss.h"   
  
@implementation Boss  
  
@synthesize delegate = _delegate;  
  
-(void)manage {  
    [_delegate draftDocuments];  
    [_delegate tellPhone];  
}  
@end  
Secretary.h
 
 
[cpp]
#import <Foundation/Foundation.h>   
#import "Boss.h"   
@interface Secretary : NSObject <MissionDelegate>  
  
@end  
 
#import <Foundation/Foundation.h>
#import "Boss.h"
@interface Secretary : NSObject <MissionDelegate>
 
@end
Secretary.m
 
 
 
[cpp] 
#import "Secretary.h"   
  
@implementation Secretary  
  
-(void)draftDocuments {  
    NSLog(@"Secretary draft documents");  
}  
  
-(void)tellPhone {  
    NSLog(@"Secretary tell phone");  
}  
  
@end

main.m
 
 
[cpp] 
 
#import <Foundation/Foundation.h>   
#import "Secretary.h"   
#import "Boss.h"   
  
int main(int argc, const char * argv[])  
{  
  
    @autoreleasepool {  
          
        // insert code here...   
        Boss *boss = [[Boss alloc] init];  
        Secretary *secretary =  [[Secretary alloc] init];  
          
        boss.delegate = secretary;  
        [boss manage];  
    }  
    return 0;  
}  
 
三、这个例子两个view视图之间传递参数 
 
定义一个MyView类,在这个视图中添加了一个button,button的事件响应他本身不处理,而让被委托类去处理,所以它就是委托类。
 
在主视图中,添加一个MyView类的实例对象,设置该实例对象的代理为self,所以它就是委托类了。
 
下面还是贴代码,应该都是很容易看懂的。

MyView.h
 
 
 
[cpp] 
#import <UIKit/UIKit.h>   
  
@protocol MyDelegate <NSObject>  
  
-(void)print:(NSString*)viewName;  
  
@end  
  
@interface MyView : UIView  
  
@property(nonatomic,assign)id<MyDelegate> mydelegate;  
  
@end  
MyView.m
 
 
 
[cpp] 
#import "MyView.h"   
  
@implementation MyView  
  
  
@synthesize mydelegate = _mydelegate;  
  
- (id)initWithFrame:(CGRect)frame  
{  
    self = [super initWithFrame:frame];  
    if (self) {  
          
        //代码创建一个button   
        UIButton *button = [UIButton buttonWithType:UIButtonTypeRoundedRect];  
        [button setTitle:@"Button" forState:UIControlStateNormal];  
        [button setFrame:CGRectMake(10, 10, 100, 50)];  
        [button setTintColor:[UIColor blueColor]];  
          
        //Target-Action模式   为button指定事件处理对象target为self,事件处理方法为buttonPressed   
        [button addTarget:self action:@selector(buttonPressed) forControlEvents:UIControlEventTouchUpInside];  
        [self addSubview:button];  
          
    }  
    return self;  
}  
//事件处理的响应方法   
-(void)buttonPressed{  
      
    [_mydelegate print:@"this is a view"];  
}  
 @end

DelegateViewController.h
 
 
[cpp] 
#import <UIKit/UIKit.h>   
#import "MyView.h"   
  
@interface DelegateViewController : UIViewController<MyDelegate>  
  
@end  
 
#import <UIKit/UIKit.h>
#import "MyView.h"
 
@interface DelegateViewController : UIViewController<MyDelegate>
 
@end
DelegateViewController.m
 
 
 
[cpp] 
#import "DelegateViewController.h"   
  
@interface DelegateViewController ()  
  
@end  
  
@implementation DelegateViewController  
  
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil  
{  
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];  
    if (self) {  
        // Custom initialization   
    }  
    return self;  
}  
  
- (void)viewDidLoad  
{  
    [super viewDidLoad];  
    // Do any additional setup after loading the view.   
    MyView *myView = [[MyView alloc]initWithFrame:CGRectMake(50, 100, 200, 100)];  
    [myView setBackgroundColor:[UIColor yellowColor]];  
    myView.mydelegate = self;  
    [self.view addSubview:myView];  
}  
  
-(void)print:(NSString *)viewName {  
    NSLog(@"%@",viewName);  
}  
  
- (void)didReceiveMemoryWarning  
{  
    [super didReceiveMemoryWarning];  
    // Dispose of any resources that can be recreated.   
}  
  
@end  

http://www.2cto.com/kf/201307/229082.html

0 0
原创粉丝点击