最近有朋友問(wèn)通知的底層怎么實(shí)現(xiàn),然后我大致的理了下:
1.通知是單例來(lái)的
2.通知可以注冊(cè)與注銷,說(shuō)明里面有數(shù)組在控制著
3.通知是要實(shí)現(xiàn)方法的,說(shuō)明里面有協(xié)議存在
4.通知是有設(shè)置通知接收者的,說(shuō)明這是使用了代理的
綜合上述說(shuō)明缕溉,我自己基于blcok寫了套自己的通知:
主要是使用兩個(gè)方法
// 接收通知
+ (void)addBlocksObject:(NotifBlock)object WithKey:(NSString *)key;
// 發(fā)送通知
+ (void)sendNotificationWithKey:(NSString *)key Info:(NSDictionary *)message;
先在需要接收的地方添加上block,然后在需要傳值出來(lái)的地方發(fā)送通知(信息通過(guò)message來(lái)傳遞)
使用方法
先對(duì)應(yīng)key添加blcok
[NotificationCenterBlock addBlocksObject:^(NSDictionary *info) {
NSLog(@"%s -- %@",__func__,info);
} WithKey:@"zzz"];
然后再對(duì)應(yīng)key發(fā)送消息
[NotificationCenterBlock sendNotificationWithKey:@"zzz" Info:@{@"JAJAAA":@"SSDDDDD",@"JAJAAAss":@"SSDDDDDss"}];
其中的key就是接收和發(fā)送的識(shí)別鑰匙吃型,也就是對(duì)應(yīng)key發(fā)送與添加block证鸥,同時(shí)這里的block是可以添加多個(gè)的。
下面是這個(gè)通知類的代碼
NotificationCenterBlock.h
//
// NotificationCenterBlock.h
// ZZZ
//
// Created by zxw on 17/5/4.
// Copyright ? 2017年 tianshikechuang. All rights reserved.
//
#import <Foundation/Foundation.h>
typedef void(^NotifBlock)(NSDictionary * info);
@interface NotificationCenterBlock : NSObject
// 發(fā)送通知
+ (void)sendNotificationWithKey:(NSString *)key Info:(NSDictionary *)message;
// 接收通知
+ (void)addBlocksObject:(NotifBlock)object WithKey:(NSString *)key;
+ (instancetype)shareNotificationCenterBlockWithKey:(NSString *)key;
+ (instancetype)NotificationCenterBlockWithKey:(NSString *)key;
- (instancetype)initNotificationCenterBlockWithKey:(NSString *)key;
- (void)sendNotificationInfo:(NSDictionary *)message;
- (void)addBlocksObject:(NotifBlock)object;
- (void)removeAllBlocksObject;
+ (void)removeAllBlocksObjectWithKey:(NSString *)key;
@end
NotificationCenterBlock.m
//
// NotificationCenterBlock.m
// ZZZ
//
// Created by zxw on 17/5/4.
// Copyright ? 2017年 tianshikechuang. All rights reserved.
//
#import "NotificationCenterBlock.h"
@interface NotificationCenterBlock ()
@property(nonatomic, strong) NSString * key;
@property(nonatomic, strong) NSMutableDictionary * keyBlockDic;
@end
@implementation NotificationCenterBlock
/* --- 單例創(chuàng)建 --- */
static NotificationCenterBlock *notif = nil;
- (instancetype)init{
if (notif) {
return notif;
}
if (self = [super init]) {
self.keyBlockDic = [NSMutableDictionary dictionary];
notif = self;
}
return self;
}
- (instancetype)initNotificationCenterBlockWithKey:(NSString *)key{
self = [[NotificationCenterBlock alloc] init];
self.key = key;
return self;
}
+ (instancetype)shareNotificationCenterBlockWithKey:(NSString *)key{
return [NotificationCenterBlock NotificationCenterBlockWithKey:key];
}
+ (instancetype)NotificationCenterBlockWithKey:(NSString *)key{
return [[NotificationCenterBlock alloc] initNotificationCenterBlockWithKey:key];
}
/* --- 發(fā)送通知 --- */
+ (void)sendNotificationWithKey:(NSString *)key Info:(NSDictionary *)message{
NotificationCenterBlock * notif = [NotificationCenterBlock shareNotificationCenterBlockWithKey:key];
[notif sendNotificationInfo:message];
}
- (void)sendNotificationInfo:(NSDictionary *)message{
NSMutableArray * arr = [self.keyBlockDic valueForKey:self.key];
if (!arr) {
return;
}
for (NotifBlock blcok in arr) {
blcok(message);
}
self.keyBlockDic[self.key] = arr;
}
/* --- 添加和移除 --- */
+ (void)addBlocksObject:(NotifBlock)object WithKey:(NSString *)key{
NotificationCenterBlock * notif = [NotificationCenterBlock shareNotificationCenterBlockWithKey:key];
[notif addBlocksObject:object];
}
+ (void)removeAllBlocksObjectWithKey:(NSString *)key{
NotificationCenterBlock * notif = [NotificationCenterBlock shareNotificationCenterBlockWithKey:key];
[notif removeAllBlocksObject];
}
- (void)addBlocksObject:(__autoreleasing NotifBlock)object{
if (!object) return;
NSMutableArray * arr = [self.keyBlockDic valueForKey:self.key];
if (!arr) {
arr = [NSMutableArray array];
}
[arr addObject:object];
self.keyBlockDic[self.key] = arr;
}
- (void)removeAllBlocksObject{
NSMutableArray * arr = [self.keyBlockDic valueForKey:self.key];
if (arr) {
[arr removeAllObjects];
}
self.keyBlockDic[self.key] = arr;
}
@end
如果有什么地方有漏洞希望大家及時(shí)指出勤晚,以便及時(shí)更改枉层!