自實現OC通知中心

//
//  NotificationCenter.h
//  Demo
//
//  Created by QzydeMac on 15/1/17.
//  Copyright (c) 2015年 Qzy. All rights reserved.
//

#import <Foundation/Foundation.h>

@interface Notification : NSObject

@property (retain,nonatomic,readwrite) NSDictionary * userInfo;
@property (assign,nonatomic) id object;
@property (assign,nonatomic) id observer;
@property (nonatomic,copy) NSString * name;
@property (nonatomic,copy) void (^callBack)();
@property (assign,nonatomic) SEL aSelector;

- (NSString *)name;
- (id)object;
- (NSDictionary *)userInfo;


+ (instancetype)notificationWithName:(NSString *)aName object:(id)anObject;
+ (instancetype)notificationWithName:(NSString *)aName object:(id)anObject userInfo:(NSDictionary *)aUserInfo;

- (instancetype)initWithName:(NSString *)name object:(id)object userInfo:(NSDictionary *)userInfo;

@end

@interface NotificationCenter : NSObject

+ (instancetype)defaultCenter;
- (void)addObserver:(id)observer callBack:(void(^)())callBack name:(NSString *)aName object:(id)anObject;

- (void)addObserver:(id)observer selector:(SEL)aSelector name:(NSString *)aName object:(id)anObject;

- (void)postNotification:(NSNotification *)notification;
- (void)postNotificationName:(NSString *)aName object:(id)anObject;
- (void)postNotificationName:(NSString *)aName object:(id)anObject userInfo:(NSDictionary *)aUserInfo;

@end




//
//  NotificationCenter.m
//  Demo
//
//  Created by QzydeMac on 15/1/17.
//  Copyright (c) 2015年 Qzy. All rights reserved.
//

#import "NotificationCenter.h"

@implementation Notification

+ (instancetype)notificationWithName:(NSString *)aName object:(id)anObject
{
    return [Notification notificationWithName:aName object:anObject userInfo:nil];
}
+ (instancetype)notificationWithName:(NSString *)aName object:(id)anObject userInfo:(NSDictionary *)aUserInfo
{
    Notification * nofi = [[Notification alloc]init];
    nofi.name = aName;
    nofi.object = anObject;
    nofi.userInfo = aUserInfo;
    return nofi;
}

- (instancetype)initWithName:(NSString *)name object:(id)object userInfo:(NSDictionary *)userInfo
{
    return [Notification notificationWithName:name object:object userInfo:userInfo];
}

@end

@implementation NotificationCenter
{
    NSMutableArray * _nofiArray;
}   
+ (instancetype)defaultCenter
{
    static NotificationCenter * _infocenter = nil;
    if (!_infocenter) {
        _infocenter = [[NotificationCenter alloc]init];
    }
    return _infocenter;
}

- (instancetype)init
{
    self = [super init];
    if (self) {
        _nofiArray = [[NSMutableArray alloc]init];
    }
    return self;
}

- (void)addObserver:(id)observer selector:(SEL)aSelector  callBack:(void (^)())callBack name:(NSString *)aName object:(id)anObject
{
    Notification * nofi = [[Notification alloc]init];
    nofi.callBack = callBack;
    nofi.name = aName;
    nofi.object = observer;
    nofi.aSelector = aSelector;
    nofi.observer = observer;
    
    [_nofiArray addObject:nofi];
}

- (void)addObserver:(id)observer callBack:(void(^)())callBack name:(NSString *)aName object:(id)anObject
{
    [self addObserver:observer selector:nil callBack:callBack name:aName object:anObject];
}

- (void)addObserver:(id)observer selector:(SEL)aSelector name:(NSString *)aName object:(id)anObject
{
    [self addObserver:observer selector:aSelector callBack:nil name:aName object:anObject];
}

- (void)postNotificationName:(NSString *)aName object:(id)anObject
{
    for (Notification * nofi in _nofiArray)
    {
        if ([nofi.name isEqualToString:aName])
        {
            
            if (nofi.callBack)
            {
                nofi.callBack();
            }
            
            if (nofi.aSelector)
            {
                if ([nofi.observer respondsToSelector:nofi.aSelector])
                {
                    [nofi.observer performSelector:nofi.aSelector withObject:nofi];
                    NSLog(@"%@",nofi.userInfo);
                }
            }
        }
    }
}

- (void)postNotification:(Notification *)notification
{
    for (Notification * nofi in _nofiArray)
    {
        if ([nofi.name isEqualToString:notification.name])
        {
            nofi.callBack = notification.callBack;
            nofi.object = notification.object;
            nofi.aSelector = notification.aSelector;
            nofi.observer = notification.observer;
            nofi.userInfo = notification.userInfo;
            break;
        }
    }
    [self postNotificationName:notification.name object:nil];
}

- (void)postNotificationName:(NSString *)aName object:(id)anObject userInfo:(NSDictionary *)aUserInfo
{
    for (Notification * nofi in _nofiArray)
    {
        if ([nofi.name isEqualToString:aName])
        {
            nofi.userInfo = aUserInfo;
            break;
        }
    }
    [self postNotificationName:aName object:nil];
}

@end
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章