Implementation Scheme 1

Use mutable arrays. The signer needs to add to the proxy’s array, and the protocol then iterates through the objects in the array for distribution. The downside is that you need the array to be strong references to its internal elements, and you need to release them in the right place, otherwise you’ll have a memory leak

  • The object of the proxy protocol. 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;

// The object needs to be destroyed in an appropriate place.
- (void)destory;
@end

NS_ASSUME_NONNULL_END

Copy the code
  • M Proxy protocol distribution mechanism
#import "TestSubView.h"

@interface TestSubView(a)

@end

@implementation TestSubView

- (instancetype)init {
    if (self = [super init]) {
        self.delegates = [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 {
    [self.delegates enumerateObjectsUsingBlock:^(id<TestSubViewDelegate>  _Nonnull delegate, NSUInteger idx, BOOL * _Nonnull stop) {
        if ([delegate respondsToSelector:@selector(testSendSomeMessageToOther:)]) {
            [delegate testSendSomeMessageToOther:@" Pass Some"]; }}]; }// Test agent distribution 2
- (void)test2DelegateAction {
    [self.delegates enumerateObjectsUsingBlock:^(id<TestSubViewDelegate>  _Nonnull delegate, NSUInteger idx, BOOL * _Nonnull stop) {
        if ([delegate respondsToSelector:@selector(testSendSome:)]) {
            [delegate testSendSome:@" pass Some2-"]; }}]; } - (void)destory {
    [self.delegates removeAllObjects];
    self.delegates = nil;
}

- (void)addDelegate:(id<TestSubViewDelegate>)delegate {
    [self.delegates addObject:delegate];
}
Copy the code
  • Signed agent 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
Copy the code
  • Parties sign 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

Copy the code

Implementation Scheme 2

Delegates will not have to worry about managing memory leaks by declaring an array of NSPointerArray because the elements in the NSPointerArray are weak. Is released as the current object is released.

There are some nshashtables and some nsmaptables and so on, which are implemented in much the same way.