OC 알림 센터
//
// NotificationCenter.h
// Demo
//
// Created by QzydeMac on 15/1/17.
// Copyright (c) 2015 Qzy. All rights reserved.
//
#import
@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
이 내용에 흥미가 있습니까?
현재 기사가 여러분의 문제를 해결하지 못하는 경우 AI 엔진은 머신러닝 분석(스마트 모델이 방금 만들어져 부정확한 경우가 있을 수 있음)을 통해 가장 유사한 기사를 추천합니다:
조건자로 필터링프로그래밍에서 컬렉션이 있을 때 요구 사항을 충족하는 부분을 필터링하고 나머지는 버려야 하는 경우가 있습니다. 자세한 예는 환자 기록 목록이 있고 미성년 환자 목록을 가져오려는 경우입니다. 이 코드를 자바 파일에 복...
텍스트를 자유롭게 공유하거나 복사할 수 있습니다.하지만 이 문서의 URL은 참조 URL로 남겨 두십시오.
CC BY-SA 2.5, CC BY-SA 3.0 및 CC BY-SA 4.0에 따라 라이센스가 부여됩니다.