通过协议实现代理设计模式。它的作用在于,一个雷需要实现特定的功能时,不需要专门自己去实现,可以由特定的类去帮忙实现。这个类可以根据功能的不同而去修改。
所以代理的好处在于
解耦合
下面用个冰箱 制冷协议来模拟一下代理模式:
制冷协议:
//
// FreezeProtocol.h
//
//
// Created by hhg on 15-6-16.
// Copyright (c) 2015年 hhg. All rights reserved.
//
#import <Foundation/Foundation.h>
@protocol FreezeProtocol <NSObject>
/// 制冷
-(void)refrigeration;
@end
冰箱:
#import <Foundation/Foundation.h>
#import "FreezeProtocol.h"
@interface Refrigerator : NSObject<FreezeProtocol>
@end
//
// Refrigerator.m
//
//
// Created by hhg on 15-6-16.
// Copyright (c) 2015年 hhg. All rights reserved.
//
#import "Refrigerator.h"
@implementation Refrigerator
-(void)refrigeration {
NSLog(@"冷藏食物");
}
@end
使用冰箱的人
//
// Person.h
//
//
// Created by hhg on 15-6-16.
// Copyright (c) 2015年 hhg. All rights reserved.
//
#import <Foundation/Foundation.h>
#import "FreezeProtocol.h"
@interface Person : NSObject
@property (nonatomic,strong)id <FreezeProtocol>refrigeratorDelegate;
- (void)toFreezeSome;
@end
//
// Person.m
//
//
// Created by hhg on 15-6-16.
// Copyright (c) 2015年 hhg. All rights reserved.
//
#import "Person.h"
@implementation Person
- (void)toFreezeSome {
NSLog(@"想吃点冷的东西,拿食物去制冷");
[_refrigeratorDelegate refrigeration];
}
@end
调用:
//
// main.m
//
//
// Created by hhg on 15-6-16.
// Copyright (c) 2015年 hhg. All rights reserved.
//
#import <Foundation/Foundation.h>
#import "Refrigerator.h"
#import "Person.h"
int main(int argc, const char * argv[]) {
@autoreleasepool {
Person *me = [[Person alloc]init];
Refrigerator *myRefrigerator = [[Refrigerator alloc]init];
[me setRefrigeratorDelegate:myRefrigerator];
[me toFreezeSome];
}
return 0;
}
当我们需要界面间反向传值时,也可以使用代理模式传值。
先来个传值协议
//
// DataProtocol.h
// DataProtocol
//
// Created by hhg on 2018/6/3.
// Copyright © 2018年 hhg. All rights reserved.
//
#import <Foundation/Foundation.h>
@protocol DataProtocol <NSObject>
-(void)postValue:(NSString *)string;
@end
找个代理
//
// NextViewController.h
// DataProtocol
//
// Created by hhg on 15/9/21.
// Copyright © 2015年 hhg. All rights reserved.
//
#import <UIKit/UIKit.h>
#import "DataProtocol.h"
@interface NextViewController : UIViewController
@property (nonatomic,strong)id<DataProtocol>Delegate;
@end
//
// NextViewController.m
// DataProtocol
//
// Created by hhg on 15/9/21.
// Copyright © 2015年 hhg. All rights reserved.
//
#import "NextViewController.h"
@implementation NextViewController
-(void)viewDidLoad {
[super viewDidLoad];
self.view.backgroundColor = [UIColor whiteColor];
NSArray *arr = [[NSArray alloc]initWithObjects:@"A",@"B",@"C",@"D", nil];
for (NSUInteger i = 1 ; i < 5 ; i++) {
UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
button.frame = CGRectMake(100, 300 + (i - 1) * 40, 80, 30);
button.backgroundColor = [UIColor colorWithRed:(arc4random() % 256 / 255.) green:(arc4random() % 256 / 255.) blue:(arc4random() % 256 / 255.) alpha:1];
[self.view addSubview:button];
[button setTitle:arr[i - 1] forState:UIControlStateNormal];
[button addTarget:self action:@selector(buttonClick:) forControlEvents:UIControlEventTouchUpInside];
}
}
-(void)buttonClick:(UIButton *)button {
[self.Delegate postValue:button.titleLabel.text];
[self dismissViewControllerAnimated:YES completion:nil];
}
@end
委托代理实现反向传值
//
// RootViewController.h
// DataProtocol
//
// Created by hhg on 15/9/21.
// Copyright © 2015年 hhg. All rights reserved.
//
#import <UIKit/UIKit.h>
#import "DataProtocol.h"
@interface RootViewController : UIViewController<DataProtocol> {
UILabel *label;
}
@end
//
// RootViewController.m
// DataProtocol
//
// Created by hhg on 15/9/21.
// Copyright © 2015年 hhg. All rights reserved.
//
#import "RootViewController.h"
#import "NextViewController.h"
@implementation RootViewController
-(void)viewDidLoad {
[super viewDidLoad];
self.view.backgroundColor = [UIColor whiteColor];
label = [[UILabel alloc]initWithFrame:CGRectMake(100, 100, 100, 100)];
label.backgroundColor = [UIColor grayColor];
label.text=@"label";
[self.view addSubview:label];
UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
button.frame = CGRectMake(200, 200, 100, 100);
button.backgroundColor=[UIColor blueColor];
[button setTitle:@"下一页" forState:UIControlStateNormal];
[self.view addSubview:button];
[button addTarget:self action:@selector(buttonClick:) forControlEvents:UIControlEventTouchUpInside];
}
// 协议方法
-(void)postValue:(NSString *)string {
label.text = string;
}
-(void)buttonClick:(UIButton *)button {
NextViewController *next = [[NextViewController alloc]init];
next.Delegate = self;
[self presentViewController:next animated:YES completion:nil];
}
@end