自实现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
时间: 2024-08-10 05:47:46

自实现OC通知中心的相关文章

OC通知中心

//通知中心广播站建立 //1.head.h #import <Foundation/Foundation.h> @interface BJBroadcast : NSObject -(void)sendBroadCast; -(void)sendBroadCastLoop; @end //2.implementation #import "BJBroadcast.h" @implementation BJBroadcast -(void)sendBroadCastLoop

iOS之NSNotificationCenter通知中心使用事项

其实这里的通知和之前说到的KVO功能很想,也是用于监听操作的,但是和KVO不同的是,KVO只用来监听属性值的变化,这个发送监听的操作是系统控制的,我们控制不了,我们只能控制监听操作,类似于Android中系统发送的广播,我们只能接受.但是通知就不一样了,他的监听发送也是又我们自己控制,我们可以在任何地方任何时机发送一个通知,类似于Android中开发者自己发送的广播.从这一点看来,通知的使用场景更为广泛了. 下面就来看一下例子: 还是护士和小孩的那个例子 Children.h [objc] vi

简化通知中心的使用

说明 1. 简化通知中心,笔者曾经实现过不需要移除的通知中心,基于弱引用集合(相对于强引用集合如NSArray,NSDictionary等)编写,本例子并无太多新意 2. 简化的通知中心由一个对象组织控制,不需要你手动移除被监听的对象,这是唯一的一点点有新意的地方 3. 在处理接收通知对象的地方,笔者提供了一个方法来简化取值操作,相对应的,发送通知消息的时候,被发送的对象也请使用指定的格式(都是用weak修饰,无需担心被持有) 源码 https://github.com/YouXianMing/

Notification Centers 通知中心

Notification Centers 通知中心 A notification center manages the sending and receiving of notifications. It notifies all observers of notifications meeting specific criteria. The notification information is encapsulated in NSNotification objects. Client o

通知中心 - NSNotificationCenter

---恢复内容开始--- NS_ASSUME_NONNULL_BEGIN /**************** Notifications ****************/ // 通知,被发送,被接受. @interface NSNotification : NSObject <NSCopying, NSCoding> 通知的名字 @property (readonly, copy) NSString *name; 具体某一个对象. @property (nullable, readonly,

第二十六篇:通知中心 NSNotificationCenter

1.通知中心(NSNotificationCenter) ?每一个应用程序都有一个通知中心(NSNotificationCenter)实例,专门负责协助不同对象之间的消息通信 ?任何一个对象都可以向通知中心发布通知(NSNotification),描述自己在做什么.其他感兴趣的对象(Observer)可以申请在某个特定通知发布时(或在某个特定的对象发布通知时)收到这个通知 2.通知(NSNotification) >一个完整的通知一般包含3个属性: - (NSString*)name; // 通

iOS开发中的错误整理,再一次整理通过通知中心来处理键盘,一定记得最后关闭通知中心

一.打开通知中心,监听键盘的显示与隐藏 二.最后记得将监听通知的对象移除

通知中心(NSNotificationCenter)

•通知机制 •掌握 •通知的发布 • •通知的监听 • •通知的移除 •通知中心(NSNotificationCenter) •每一个应用程序都有一个通知中心(NSNotificationCenter)实例,专门负责协助不同对象之间的消息通信 •任何一个对象都可以向通知中心发布通知(NSNotification),描述自己在做什么.其他感兴趣的对象(Observer)可以申请在某个特定通知发布时(或在某个特定的对象发布通知时)收到这个通知 •通知(NSNotification) •一个完整的通知

iOS通知中心 NSNotificationCenter详解

NSNotificationCenter的适用场景,原理机制,使用步骤等. 通知中心的使用顺序:先确保注册了观察者,因为发送通知是一瞬间的事,如果没有注册观察者,发送通知后再注册是不会收到的. 总结:通知只会发送给当前监听着的对象. 代码 //注册通知  在关心该通知的页面注册监听 [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(tongzhi:) name:@"tongzhi" obj