首先總結(jié)一下通知中心(NSNotificationCenter)的使用;
NSNotificationCenter 使用起來(lái)的還是非常簡(jiǎn)單的:這里不對(duì)NSNotificationCenter 進(jìn)行過(guò)多解讀,有很多大神的文章大家可以去看。
1:添加通知中心
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(notice:) name:@"123" object:nil];
2:發(fā)送通知
[[NSNotificationCenter defaultCenter] postNotificationName:@"123" object:nil];
3:移除通知 (這個(gè)很重要)
[[NSNotificationCenter defaultCenter] removeObserver:self name:@"123" object:nil];
蘋(píng)果粑粑多好啊,給我用的東西都是那么的簡(jiǎn)潔大方。哈哈
但是還是有一些同學(xué)十分的嫌棄它,為什么要remove呢?就不能讓我少寫(xiě)點(diǎn)嗎?
對(duì),我就是那個(gè)同學(xué)。
下邊自己寫(xiě)了一個(gè)通知中心,很簡(jiǎn)單代碼不多,還需完善的地方。請(qǐng)多指教;
有類(lèi)個(gè)類(lèi)(操作類(lèi))BaseNotice和(model類(lèi))NoticeModel
先看 BaseNotice.h
@interface BaseNotice : NSObject
+ (instancetype)getInstance;
- (void)postNotificationName:(NSString *)aName object:(id)anObject;
- (void)addObserverName:(NSString *)aName notice:(void(^)(id anObject))notice;
- (void)acceptObserverName:(NSString *)aName appearNotice:(void(^)(id anObject))notice;
@end
NoticeModel 沒(méi)什么說(shuō)的。
@interface NoticeModel : NSObject
@property(nonatomic,copy)NSString *name;
@property(nonatomic,strong)id object;
@end
然后是BaseNotice.m 代碼貼上去有點(diǎn)長(zhǎng)所以放到最后,先看使用方式吧!
首先在A ViewController中添加通知中心
[[BaseNotice getInstance] addObserverName:@"123" notice:^(id anObject) {
NSLog(@"123");
}];
再者來(lái)到B ViewController中發(fā)送通知,這樣就可以了。
[[BaseNotice getInstance] postNotificationName:@"123" object:nil];
那么不需要remove嗎?發(fā)送通知的原理是基于setter 方法的,每個(gè)通知model都存在于dic里面。
如果你的項(xiàng)目使用通知中心的頻率非常高你應(yīng)該換種方式了,通知中心使用的太多會(huì)造成很多未知bug。
比如通知未釋放多次add,那么接受到的通知就是多個(gè)。
還有一個(gè)方法沒(méi)有使用是干什么的呢?
[[BaseNotice getInstance] acceptObserverName:@"123" appearNotice:^(id anObject) {
}];
這個(gè)方法是一對(duì)一的單次通知,這個(gè)方法需要主動(dòng)去掉用才會(huì)執(zhí)行。是可以先post通知,再去接受的。
accept 過(guò)一次必須從新post 才可以收到通知??梢赃@樣用:
- (void)viewWillAppear:(BOOL)animated{
[super viewWillAppear:animated];
[[BaseNotice getInstance] acceptObserverName:@"123" appearNotice:^(id anObject) {
}];
}
當(dāng)你從B ViewController 返回時(shí),通知告訴他do someting.
之前的項(xiàng)目里面遇到的返回的時(shí)候,是否需要刷新數(shù)據(jù),你就可以這么用。收到通知才會(huì)to do.
//下面是BaseNotice.m 里面的代碼:
typedef void (^NoticeBlock)(id object);
@interface BaseNotice()
@property(nonatomic,strong)NSMutableDictionary *nameDic;
@property(nonatomic,copy)NSString *aName;
@property(nonatomic,copy)NoticeBlock nBlock;
@end
@implementation BaseNotice
+ (instancetype)getInstance {
static id instance = nil;
static dispatch_once_t onceToken;
dispatch_once(&onceToken, ^{
instance = [[self alloc] init];
});
return instance;
}
- (void)postNotificationName:(NSString *)aName object:(id)anObject{
if (aName.length <= 0) {
return;
}
if ([self.nameDic objectForKey:aName]) {
NoticeModel *model = [self.nameDic objectForKey:aName];
model.object = anObject;
self.aName = aName;
}else{
NoticeModel *model = [[NoticeModel alloc]init];
model.name = aName;
model.object = anObject;
[self.nameDic setObject:model forKey:aName];
}
}
- (void)addObserverName:(NSString *)aName notice:(void(^)(id anObject))notice{
if (aName.length <= 0) {
return;
}
if ([self.nameDic objectForKey:aName]) {
return;
}
NoticeModel *model = [[NoticeModel alloc]init];
model.name = aName;
[self.nameDic setObject:model forKey:aName];
if (notice){
self.nBlock = notice;
}
}
//單次通知(需要主動(dòng)運(yùn)行)
- (void)acceptObserverName:(NSString *)aName appearNotice:(void(^)(id anObject))notice{
if (aName.length <= 0) {
return;
}
if ([self.nameDic objectForKey:aName]) {
NoticeModel *model = [self.nameDic objectForKey:aName];
if (notice) {
notice(model.object);
}
//用完清除
[self.nameDic removeObjectForKey:aName];
}
}
- (void)setAName:(NSString *)aName{
if (aName.length <= 0) {
return;
}
_aName = aName;
if ([self.nameDic objectForKey:aName]) {
NoticeModel *model = [self.nameDic objectForKey:aName];
self.nBlock(model.object);
}
}
- (NSMutableDictionary *)nameDic{
if (_nameDic == nil) {
_nameDic = [NSMutableDictionary dictionary];
}
return _nameDic;
}
@end
代碼不多也很簡(jiǎn)單,需要完善的地方很多。請(qǐng)多指教。
大家喜歡的話(huà),我會(huì)寫(xiě)出完整版。供大家下載使用。