您的位置:首页 > 运维架构

自实现OC通知中心

2015-01-18 13:48 190 查看
//
//  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
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息