Objective C 语言 自定义通知中心

Objective-C阿木 发布于 17 天前 5 次阅读


Objective-C 自定义通知中心实现与使用详解

在iOS开发中,通知中心(NotificationCenter)是一个强大的机制,用于在不同对象之间传递消息和事件。系统提供的通知中心可能无法满足所有场景的需求。本文将围绕Objective-C语言,探讨如何自定义通知中心,并详细介绍其实现和使用方法。

自定义通知中心的需求

在以下场景中,我们可能需要自定义通知中心:

1. 需要更细粒度的消息传递控制。

2. 需要跨多个应用程序或框架的消息传递。

3. 需要自定义消息的发送和接收方式。

自定义通知中心的实现

1. 定义通知和通知中心

我们需要定义通知和通知中心的基本结构。

objective-c

@interface Notification : NSObject

@property (nonatomic, strong) NSString name;


@property (nonatomic, strong) NSDictionary userInfo;

- (instancetype)initWithName:(NSString )name userInfo:(NSDictionary )userInfo;

@end

@interface NotificationCenter : NSObject

+ (instancetype)defaultCenter;


- (void)postNotificationName:(NSString )name object:(id)object userInfo:(NSDictionary )userInfo;

@end


2. 实现通知中心

接下来,我们实现通知中心的逻辑。

objective-c

@interface NotificationCenter ()

@property (nonatomic, strong) NSMutableDictionary<NSString , NSMutableArray<NSNotificationCenter > > observers;

@end

@implementation NotificationCenter

+ (instancetype)defaultCenter {


static id _defaultCenter = nil;


static dispatch_once_t onceToken;


dispatch_once(&onceToken, ^{


_defaultCenter = [[self alloc] init];


});


return _defaultCenter;


}

- (instancetype)init {


self = [super init];


if (self) {


_observers = [NSMutableDictionary dictionary];


}


return self;


}

- (void)postNotificationName:(NSString )name object:(id)object userInfo:(NSDictionary )userInfo {


NSMutableArray<NSNotificationCenter > notifiers = _observers[name];


if (!notifiers) {


return;


}



for (NSNotificationCenter notifier in notifiers) {


[notifier postNotificationName:name object:object userInfo:userInfo];


}


}

- (void)addObserver:(NSNotificationCenter )notifier forName:(NSString )name object:(id)object {


NSMutableArray<NSNotificationCenter > notifiers = _observers[name];


if (!notifiers) {


notifiers = [NSMutableArray array];


_observers[name] = notifiers;


}


[notifiers addObject:notifier];


}

- (void)removeObserver:(NSNotificationCenter )notifier forName:(NSString )name object:(id)object {


NSMutableArray<NSNotificationCenter > notifiers = _observers[name];


if (!notifiers) {


return;


}



[notifiers removeObject:notifier];


}

@end


3. 使用自定义通知中心

现在,我们可以使用自定义通知中心来发送和接收通知。

objective-c

// 创建通知


Notification notification = [[Notification alloc] initWithName:@"myNotification" userInfo:@{@"key": @"value"}];

// 发送通知


[NotificationCenter.defaultCenter postNotificationName:@"myNotification" object:nil userInfo:notification.userInfo];

// 注册观察者


NSNotificationCenter observer = [[NSNotificationCenter defaultCenter] addObserverForName:@"myNotification" object:nil queue:[NSOperationQueue mainQueue] usingBlock:^(NSNotification notification) {


NSLog(@"Received notification with key: %@", notification.userInfo[@"key"]);


}];

// 取消注册观察者


[observer removeObserverForName:@"myNotification" object:nil];


总结

通过自定义通知中心,我们可以更好地控制消息的传递和接收。本文详细介绍了Objective-C中自定义通知中心的实现和使用方法,希望对您有所帮助。

扩展阅读

1. [Objective-C Notifications](https://developer.apple.com/documentation/objectivec/nsnotificationcenter)

2. [Objective-C Blocks](https://developer.apple.com/documentation/objectivec/nsblock)

3. [Objective-C Memory Management](https://developer.apple.com/documentation/objectivec/memory_management)

以上内容仅为3000字左右,如需进一步扩展,请参考相关资料。