iOS 通用代理的写法 一个代理添加多个执行函数

/*添加代理,当不使用时记得移除*/
- (void)addDelegate:(id<HXFriendRelationManagerDelegate>)delegate {

    @synchronized (self.delegates) {
        if (![self.delegates containsObject:delegate]) {
            [self.delegates addObject:delegate];
        }
    }
}
//移除代理
- (void)removeDelegate:(id<HXFriendRelationManagerDelegate>)delegate {
    @synchronized (self.delegates) {
        [self.delegates removeObject:delegate];
    }
}

//代理的执行

- (void)notFriendListChange {

    @synchronized (self.delegates) {
        
        dispatch_async(dispatch_get_main_queue(), ^{
            for (id<HXFriendRelationManagerDelegate> delegate in self.delegates) {
                if ([delegate respondsToSelector:@selector(hxFriendListChange)]) {
                    [delegate performSelector:@selector(hxFriendListChange)];
                }
            }
  
        });
    }
}
//代理的执行
- (void)notADDFriendMsg:(EMMessage *)msg {
    
    @synchronized (self.delegates) {
        for (id<HXFriendRelationManagerDelegate> delegate in self.delegates) {
            if ([delegate respondsToSelector:@selector(getAddFriendMessage:)]) {
                [delegate performSelector:@selector(getAddFriendMessage:)withObject:msg];
            }
        }
    }

    
}


你可能感兴趣的:(iOS 通用代理的写法 一个代理添加多个执行函数)