时间:2022-03-25 09:40:17 | 栏目: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 等等, 实现方式大同小异。