• 自动移除的通知中心


    自动移除的通知中心

    源码

    //
    //  DefaultNotificationCenter.h
    //  TotalCustomTabBarController
    //
    //  Created by YouXianMing on 16/6/3.
    //  Copyright © 2016年 YouXianMing. All rights reserved.
    //
    
    #import <Foundation/Foundation.h>
    @class DefaultNotificationCenter;
    
    @protocol DefaultNotificationCenterDelegate <NSObject>
    
    @required
    
    /**
     *  DefaultNotificationCenter's event.
     *
     *  @param notification DefaultNotificationCenter object.
     *  @param name         Event name.
     *  @param object       Event object, maybe nil.
     */
    - (void)defaultNotificationCenter:(DefaultNotificationCenter *)notification name:(NSString *)name object:(id)object;
    
    @end
    
    @interface DefaultNotificationCenter : NSObject
    
    /**
     *  Post event to notification name.
     *
     *  @param name   Notification name.
     *  @param object Data.
     */
    + (void)postEventToNotificationName:(NSString *)name object:(id)object;
    
    /**
     *  DefaultNotificationCenter's delegate.
     */
    @property (nonatomic, weak) id <DefaultNotificationCenterDelegate>  delegate;
    
    /**
     *  Add notification name.
     *
     *  @param name Notification name.
     */
    - (void)addNotificationName:(NSString *)name;
    
    /**
     *  Delete notification name.
     *
     *  @param name Notification name.
     */
    - (void)deleteNotificationName:(NSString *)name;
    
    /**
     *  Get all the notification names.
     *
     *  @return Notification names's array.
     */
    - (NSArray <NSString *> *)notificationNames;
    
    /**
     *  Remove all notifications.
     */
    - (void)removeAllNotifications;
    
    @end
    //
    //  DefaultNotificationCenter.m
    //  TotalCustomTabBarController
    //
    //  Created by YouXianMing on 16/6/3.
    //  Copyright © 2016年 YouXianMing. All rights reserved.
    //
    
    #import "DefaultNotificationCenter.h"
    
    @interface DefaultNotificationCenterModel : NSObject
    
    @property (nonatomic, strong) NSString *name;
    @property (nonatomic)         BOOL      effective;
    
    @end
    
    @implementation DefaultNotificationCenterModel
    
    @end
    
    @interface DefaultNotificationCenter ()
    
    @property (nonatomic, strong) NSMutableArray <DefaultNotificationCenterModel *> *stringsArray;
    
    @end
    
    @implementation DefaultNotificationCenter
    
    - (instancetype)init {
        
        if (self = [super init]) {
            
            self.stringsArray = [NSMutableArray array];
        }
        
        return self;
    }
    
    + (void)postEventToNotificationName:(NSString *)name object:(id)object {
    
        [[NSNotificationCenter defaultCenter] postNotificationName:name object:object];
    }
    
    - (void)addNotificationName:(NSString *)name {
    
        // Check have the same name or not.
        __block BOOL haveTheSameName = NO;
        
        [self.stringsArray enumerateObjectsUsingBlock:^(DefaultNotificationCenterModel * _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
            
            if ([obj.name isEqualToString:name]) {
                
                haveTheSameName = YES;
                *stop           = YES;
            }
        }];
        
        // Add notification.
        if (haveTheSameName == NO) {
            
            DefaultNotificationCenterModel *model = [DefaultNotificationCenterModel new];
            model.name                            = name;
            model.effective                       = YES;
            [self.stringsArray addObject:model];
            
            [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(notificationEvent:) name:model.name object:nil];
        }
    }
    
    - (void)deleteNotificationName:(NSString *)name {
        
        // Check have the same name or not.
        __block BOOL      haveTheSameName = NO;
        __block NSInteger index           = 0;
        
        [self.stringsArray enumerateObjectsUsingBlock:^(DefaultNotificationCenterModel * _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
            
            if ([obj.name isEqualToString:name]) {
                
                haveTheSameName = YES;
                index           = idx;
                *stop           = YES;
            }
        }];
        
        // Remove notification.
        if (haveTheSameName == YES) {
            
            DefaultNotificationCenterModel *model = self.stringsArray[index];
            [[NSNotificationCenter defaultCenter] removeObserver:self name:model.name object:nil];
            [self.stringsArray removeObject:model];
        }
    }
    
    - (NSArray <NSString *> *)notificationNames {
    
        NSMutableArray *namesArray = [NSMutableArray array];
        [self.stringsArray enumerateObjectsUsingBlock:^(DefaultNotificationCenterModel * _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
            
            [namesArray addObject:obj.name];
        }];
        
        return [NSArray arrayWithArray:namesArray];
    }
    
    - (void)removeAllNotifications {
    
        [self.stringsArray enumerateObjectsUsingBlock:^(DefaultNotificationCenterModel * _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
            
            [[NSNotificationCenter defaultCenter] removeObserver:self name:obj.name object:nil];
        }];
        
        [self.stringsArray removeAllObjects];
    }
    
    - (void)notificationEvent:(id)obj {
    
        NSNotification *notification = obj;
    
        if (self.delegate && [self.delegate respondsToSelector:@selector(defaultNotificationCenter:name:object:)]) {
            
            [self.delegate defaultNotificationCenter:self name:notification.name object:notification.object];
        }
    }
    
    - (void)dealloc {
    
        [self removeAllNotifications];
    }
    
    @end

    细节

    1. 将通知的接收转换成了代理,根据代理中的一个通知名字值来区分不同的通知.

    2. 不用自动移除注册的通知

    3. 用这个方法发送通知

  • 相关阅读:
    深入浅出的webpack4构建工具--webpack4+vue+route+vuex项目构建(十七)
    深入浅出的webpack4构建工具---Scope Hoisting(十六)
    深入浅出的webpack4构建工具---webpack+vue+router 按需加载页面(十五)
    深入浅出的webpack构建工具--webpack4+vue+router项目架构(十四)
    关于html的a标签的target="__blank "的安全漏洞问题
    深入浅出的webpack构建工具--webpack4+vue搭建环境 (十三)
    深入浅出的webpack构建工具---tree shaking打包性能优化(十二)
    深入浅出的webpack4构建工具---浏览器前端资源缓存(十一)
    vue分页全选和单选操作
    go语言之进阶篇通过range遍历channel内容
  • 原文地址:https://www.cnblogs.com/YouXianMing/p/5574369.html
Copyright © 2020-2023  润新知