详解iOS 实现一对多代理方案
目录
- 实现方案一
- 实现方案二
实现方案一
利用可变数组。 签协议方需要add到代理的数组中, 然后协议遍历数组中的对象,进行分发。
缺点是需要数组对其内部元素是强引用, 需要在合适的地方对其进行释放,否则会有内存泄漏
代理协议的对象.h写法
#import <UIKit/UIKit.h> NS_ASSUME_NONNULL_BEGIN @protocol TestSubViewDelegate <NSObject> - (void)testSendSomeMessageToOther:(NSString *)somethings; - (void)testSendSome:(NSString *)somethings; @end @interface TestSubView : UIView //@property (nonatomic, weak)id <TestSubViewDelegate>delegate; @property (nonatomic, strong)NSMutableArray <id<TestSubViewDelegate>>* __nullable delegates; - (void)addDelegate:(id<TestSubViewDelegate>)delegate; // 需要在合适的地方销毁对象。 - (void)destory; @end NS_ASSUME_NONNULL_END
.m代理协议分发机制
#import "TestSubView.h" @interface TestSubView () @end @implementation TestSubView - (instancetype)init { if (self = [super init]) { self.delegates = [NSMutableArray array]; // 测试,执行 dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(2 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{ [self test1DelegateAction]; }); dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(5 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{ [self test2DelegateAction]; }); } return self; } // 测试代理方法分发1 - (void)test1DelegateAction { [self.delegates enumerateObjectsUsingBlock:^(id<TestSubViewDelegate> _Nonnull delegate, NSUInteger idx, BOOL * _Nonnull stop) { if ([delegate respondsToSelector:@selector(testSendSomeMessageToOther:)]) { [delegate testSendSomeMessageToOther:@"传递的Some"]; } }]; } // 测试代理分发2 - (void)test2DelegateAction { [self.delegates enumerateObjectsUsingBlock:^(id<TestSubViewDelegate> _Nonnull delegate, NSUInteger idx, BOOL * _Nonnull stop) { if ([delegate respondsToSelector:@selector(testSendSome:)]) { [delegate testSendSome:@"传递的Some2-"]; } }]; } - (void)destory { [self.delegates removeAllObjects]; self.delegates = nil; } - (void)addDelegate:(id<TestSubViewDelegate>)delegate { [self.delegates addObject:delegate]; }
签订代理方1
#import "View1Controller.h" #import "TestSubView.h" @interface View1Controller ()<TestSubViewDelegate> @end @implementation View1Controller - (void)viewDidLoad { [super viewDidLoad]; TestSubView *ts = [TestSubView new]; [ts addDelegate:self]; [self.view addSubview:ts]; } #pragma mark - TestSubViewDelegate - (void)testSendSomeMessageToOther:(NSString *)somethings { NSLog(@"%@", somethings); } - (void)testSendSome:(NSString *)somethings { NSLog(@"%@", somethings); } @end
签订方2
#import "ViewController.h" #import "View1Controller.h" #import "TestSubView.h" @interface ViewController ()<TestSubViewDelegate> @end @implementation ViewController - (void)viewDidLoad { [super viewDidLoad]; TestSubView *ts = [TestSubView new]; [ts addDelegate:self]; [self.view addSubview:ts]; } - (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event { View1Controller *vc = [View1Controller new]; [self presentViewController:vc animated:YES completion:nil]; } #pragma mark - TestSubViewDelegate - (void)testSendSomeMessageToOther:(NSString *)somethings { NSLog(@"%@", somethings); } @end
实现方案二
采用NSPointerArray去声明delegates的数组,这样就可以不用操心管理内存泄漏的问题, 因为NSPointerArray里面的元素都是weak化的。 会随着当前对象释放而释放掉。
还有一些NSHashTable NSMapTable 等等, 实现方式大同小异。
到此这篇关于详解iOS 实现一对多代理方案的文章就介绍到这了,更多相关iOS 一对多代理内容请搜索我们以前的文章或继续浏览下面的相关文章希望大家以后多多支持我们!
赞 (0)