Implementation plan one
Use variable arrays. The party signing the agreement needs to add to the proxy array, and then the protocol traverses the objects in the array and distributes it.
The disadvantage is that the array needs to have a strong reference to its internal elements, and it needs to be released in the right place, otherwise there will be a memory leak.
How to write the object of the proxy protocol.h
#import <UIKit/> 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; // Objects need to be destroyed in the right place.- (void)destory; @end NS_ASSUME_NONNULL_END
.m proxy protocol distribution mechanism
#import "" @interface TestSubView () @end @implementation TestSubView - (instancetype)init { if (self = [super init]) { = [NSMutableArray array]; // Test, execute 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; } // Test proxy method distribution 1- (void)test1DelegateAction { [ enumerateObjectsUsingBlock:^(id<TestSubViewDelegate> _Nonnull delegate, NSUInteger idx, BOOL * _Nonnull stop) { if ([delegate respondsToSelector:@selector(testSendSomeMessageToOther:)]) { [delegate testSendSomeMessageToOther:@"Some passed"]; } }]; } // Test Agent Distribution 2- (void)test2DelegateAction { [ enumerateObjectsUsingBlock:^(id<TestSubViewDelegate> _Nonnull delegate, NSUInteger idx, BOOL * _Nonnull stop) { if ([delegate respondsToSelector:@selector(testSendSome:)]) { [delegate testSendSome:@"Some2- passed"]; } }]; } - (void)destory { [ removeAllObjects]; = nil; } - (void)addDelegate:(id<TestSubViewDelegate>)delegate { [ addObject:delegate]; }
Signing agent 1
#import "" #import "" @interface View1Controller ()<TestSubViewDelegate> @end @implementation View1Controller - (void)viewDidLoad { [super viewDidLoad]; TestSubView *ts = [TestSubView new]; [ts addDelegate:self]; [ addSubview:ts]; } #pragma mark - TestSubViewDelegate - (void)testSendSomeMessageToOther:(NSString *)somethings { NSLog(@"%@", somethings); } - (void)testSendSome:(NSString *)somethings { NSLog(@"%@", somethings); } @end
Signing Party 2
#import "" #import "" #import "" @interface ViewController ()<TestSubViewDelegate> @end @implementation ViewController - (void)viewDidLoad { [super viewDidLoad]; TestSubView *ts = [TestSubView new]; [ts addDelegate:self]; [ 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
Implementation Plan II
Use NSPointerArray to declare the array of delegates, so that you don’t have to worry about managing memory leaks, because the elements in NSPointerArray are weak. Will be released as the current object is released.
There are also some NSHashTable NSMapTable, etc., and the implementation methods are similar.
This is the end of this article about detailed explanation of iOS's implementation of one-to-many proxy solution. For more related iOS one-to-many proxy content, please search for my previous articles or continue browsing the related articles below. I hope everyone will support me in the future!