美文网首页iOS
iOS 通知中心不好用?那就自己写一个

iOS 通知中心不好用?那就自己写一个

作者: wangyongyue | 来源:发表于2018-07-26 16:31 被阅读5次

首先总结一下通知中心(NSNotificationCenter)的使用;

NSNotificationCenter 使用起来的还是非常简单的:这里不对NSNotificationCenter 进行过多解读,有很多大神的文章大家可以去看。

1:添加通知中心

[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(notice:) name:@"123" object:nil];

2:发送通知

[[NSNotificationCenter defaultCenter] postNotificationName:@"123" object:nil];

3:移除通知 (这个很重要)

[[NSNotificationCenter defaultCenter] removeObserver:self name:@"123" object:nil];

苹果粑粑多好啊,给我用的东西都是那么的简洁大方。哈哈

但是还是有一些同学十分的嫌弃它,为什么要remove呢?就不能让我少写点吗?

对,我就是那个同学。

下边自己写了一个通知中心,很简单代码不多,还需完善的地方。请多指教;
有类个类(操作类)BaseNotice和(model类)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 没什么说的。

@interface NoticeModel : NSObject
@property(nonatomic,copy)NSString *name;
@property(nonatomic,strong)id object;
@end

然后是BaseNotice.m 代码贴上去有点长所以放到最后,先看使用方式吧!
首先在A ViewController中添加通知中心

 [[BaseNotice getInstance] addObserverName:@"123" notice:^(id anObject) {
        
        NSLog(@"123");
    }];

再者来到B ViewController中发送通知,这样就可以了。

[[BaseNotice getInstance] postNotificationName:@"123" object:nil];

那么不需要remove吗?发送通知的原理是基于setter 方法的,每个通知model都存在于dic里面。
如果你的项目使用通知中心的频率非常高你应该换种方式了,通知中心使用的太多会造成很多未知bug。
比如通知未释放多次add,那么接受到的通知就是多个。

还有一个方法没有使用是干什么的呢?

[[BaseNotice getInstance] acceptObserverName:@"123" appearNotice:^(id anObject) {
        
    }];

这个方法是一对一的单次通知,这个方法需要主动去掉用才会执行。是可以先post通知,再去接受的。
accept 过一次必须从新post 才可以收到通知。可以这样用:

- (void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
    [[BaseNotice getInstance] acceptObserverName:@"123" appearNotice:^(id anObject) {
        
    }];
}

当你从B ViewController 返回时,通知告诉他do someting.
之前的项目里面遇到的返回的时候,是否需要刷新数据,你就可以这么用。收到通知才会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;
    }
    
}
//单次通知(需要主动运行)
- (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

代码不多也很简单,需要完善的地方很多。请多指教。
大家喜欢的话,我会写出完整版。供大家下载使用。

相关文章

  • iOS 通知中心不好用?那就自己写一个

    首先总结一下通知中心(NSNotificationCenter)的使用; NSNotificationCenter...

  • IOS通知中心

    通知中心(NSnotificationCenter): 由发送者1或者多个发送者n 通过发送一个通知到通知中心,接...

  • iOS - 通知中心

    通知中心:NSNotificationCentereg:农村大喇叭发送广播 条件:在大喇叭广播之前要有村民(对象-...

  • iOS 通知中心

    不要随随便便就是用通知,不懂的话对性能影响挺大的.虽然用起来很方便. 下边是理论知识: 即使在适合使用通告的场合下...

  • iOS 通知中心

    What is NSNotificationCenter? NSNotificationCenter是一种一对多的...

  • iOS通知中心

    http://potter528.bj.bdysite.com 如果播放器播放完毕后通知中心通知对象,执行方法选择...

  • iOS通知中心

    有关的类 NSNotification NSNotificationCenter NSNotification N...

  • iOS中的通知(NSNotification)

    通知中心是一个单例。通知在iOS中是一种设计模式。每一个应用程序都有一个通知中心NSNotificationCen...

  • iOS 通知 与 通知中心

    iOS 通知中心:自己实现了一套消息机制,可以跨页面调用 类似Unity的SendMessage,是订阅、发布者模...

  • IOS 通知中心NotificationCenter

网友评论

    本文标题:iOS 通知中心不好用?那就自己写一个

    本文链接:https://www.haomeiwen.com/subject/wvmbmftx.html