码迷,mamicode.com
首页 > 其他好文 > 详细

不用自动移除的通知中心

时间:2016-06-10 23:01:55      阅读:272      评论:0      收藏:0      [点我收藏+]

标签:

不用自动移除的通知中心

技术分享

 

源码

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

技术分享

 

不用自动移除的通知中心

标签:

原文地址:http://www.cnblogs.com/YouXianMing/p/5574369.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!