前言:代理delegate只能实现“一对一”的通信,也即一般情况下,一个委托方只能有一个代理方,多次设置只是代理的替换,并不能实现多个代理;但有时我们却有需要多个代理的需求,这个时候该怎么办呢?
“多播代理”应用而生,实现机制如下:
方式一:
#import
@protocol ViewControllerDelegate
- (void)test;
@end
@interface ViewController : UIViewController
/** 添加代理至数组中 */
- (void)addDelegate:(id)delegate;
@end
----------------ViewControllerBridge.h/.m文件-------------------
.m文件中声明了一个小文件:桥文件
#import "ViewController.h"
@interface ViewControllerBridge:NSObject
@property (nonatomic, retain) iddelegate;
@end
@implementation ViewControllerBridge
- (void)dealloc{
NSLog(@"-------dealloc: bridge-------");
}
@end
----------------------ViewController.m 文件----------------------
#import "SecondController.h"
#import "ThirdController.h"
@interface ViewController ()
@property (nonatomic, retain) NSMutableArray *delegates;
@property (nonatomic, retain) SecondController *svc;
@property (nonatomic, retain) ThirdController *tvc;
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
self.svc = [[SecondController alloc]init];
self.tvc = [[ThirdController alloc]init];
[self addDelegate:self.svc];
[self addDelegate:self.tvc];
}
-(void)addDelegate:(id)delegate{
ViewControllerBridge *bridge = [[ViewControllerBridge alloc]init];
bridge.delegate = delegate;
[self.delegates addObject:bridge];
}
- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event{
for (ViewControllerBridge *bridge in self.delegates) {
if (bridge.delegate && [bridge.delegate respondsToSelector:@selector(test)]) {
[bridge.delegate test];
}
}
}
- (NSMutableArray *)delegates{
if (!_delegates) {
_delegates = [NSMutableArray array];
}
return _delegates;
}
- (void)dealloc{
NSLog(@"-------dealloc: ViewController-------");
}
@end
核心实现方式:
不是delegate的多次设置,而是添加一个中间文件,每次创建一个新对象,并将代理绑定到该对象中,而后再添加至委托的【代理】数组中。发送消息时,遍历该数组,“剥离出”被绑定的代理,而后执行协议方法即可。
思考:为什么将新文件置于主题文件中???
当然独立出来也可以实现相同的效果,前者主要是为了更加紧凑而已。
----------------------ViewController.h 文件----------------------
#import
#import "ViewController.h"
@interface BridgeDelegate : NSObject
@property (nonatomic, weak) iddelegate;
@end
----------------------ViewController.m 文件----------------------
#import "BridgeDelegate.h"
@implementation BridgeDelegate
- (void)dealloc{
NSLog(@"-------dealloc:BridgeDelegate----------");
}
@end
方式二:
----------------------CHJSecondController.h 文件----------------------
#import
@protocol CHJSecondControllerDelegate
@optional
- (void)test;
@end
@interface CHJSecondController : UIViewController
@end
----------------------CHJSecondController.m 文件----------------------
#import "CHJSecondController.h"
#import "CHJMultiDelegate.h"
#import "Dog.h"
#import "Cat.h"
#import "Pig.h"
@interface CHJSecondController ()
@property (nonatomic, strong) NSMutableArray *delegates;
@property (nonatomic, retain) CHJMultiDelegate *multiDelegate;
@property (nonatomic, retain) Dog *dog;
@property (nonatomic, retain) Cat *cat;
@property (nonatomic, retain) Pig *pig;
@end
@implementation CHJSecondController
- (void)viewDidLoad {
[super viewDidLoad];
self.view.backgroundColor = [UIColor greenColor];
self.multiDelegate = [[CHJMultiDelegate alloc]init];
self.dog = [[Dog alloc]init];
self.cat = [[Cat alloc]init];
self.pig = [[Pig alloc]init];
[self.multiDelegate addDelegate:self.dog];
[self.multiDelegate addDelegate:self.cat];
[self.multiDelegate addDelegate:self.pig];
self.delegates = self.multiDelegate.delegates;
}
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
for (id delegate in self.delegates) {
BOOL isTrue = delegate && [delegate respondsToSelector:@selector(test)];
NSLog(@"result = %d",isTrue);
if (isTrue) {
[delegate test];
}
}
}
- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event{
[self dismissViewControllerAnimated:YES completion:nil];
}
- (void)dealloc{
NSLog(@"dealloc:%@被释放了!",self);
}
@end
----------------------CHJMultiDelegate.h 文件----------------------
#import
#import "ViewController.h"
@interface CHJMultiDelegate : NSObject
@property (nonatomic, readonly) NSPointerArray *delegates;
- (void)addDelegate:(id)delegate;
- (void)deleteDelegate:(id)delegate;
@end
----------------------CHJMultiDelegate.m 文件----------------------
- (instancetype)init {
self = [super init];
if (self) {
_delegates = [NSPointerArray weakObjectsPointerArray];
}
return self;
}
- (void)addDelegate:(id)delegate{
if (!delegate) return;
[_delegates addPointer:(__bridge void *)delegate];
}
- (void)deleteDelegate:(id)delegate{
for (int i = 0; i < _delegates.count; i++) {
id instance = [_delegates pointerAtIndex:i];
if (instance == (__bridge void *)delegate) {
[_delegates removePointerAtIndex:i];
}
[_delegates compact];
}
}