zoukankan      html  css  js  c++  java
  • 自动移除的通知中心

    自动移除的通知中心

    源码

    //
    //  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. 用这个方法发送通知

  • 相关阅读:
    SQL Server 数据类型映射 (ADO.NET)
    微软SQLHelper.cs类 中文版
    在WinForm中使用Web Service来实现软件自动升级
    Winform开发框架之通用自动更新模块(转)
    C# winform 最小化到电脑右下角
    3层数据访问架构(部分)
    Castle ActiveRecord学习实践
    .Net下的 ORM框架介紹
    配置企业库5.0管理
    一个简洁通用的调用DLL函数的帮助类
  • 原文地址:https://www.cnblogs.com/YouXianMing/p/5574369.html
Copyright © 2011-2022 走看看