通过NSNotificationCenter传递信息

iOS通过NSNotificationCenter来传递通告(Notification)的原理非常简单。

俗一点的原理如下:这个电线杆就是一个NotificationCenter. 谁都可以来发通告(Notification),谁都可以来看通告(Notification)。

NotificationCenter
NotificationCenter

雅一点的原理如下:

学术一点
学术一点

先讲讲俗的原理

电线杆上的牛皮癣广告包含了至少三个参与方:

  1. 电线杆 (充当NSNotificationCenter的角色)
  2. 有祖传老中医的广告张贴者 (充当Poster的角色)
  3. 有祖传牛皮癣的路人甲乙丙丁 (充当Observer的角色)

发送notification(贴小广告的来了)

方法一不用传递更多的信息,函数如下:

[NSNotificationCenter defaultCenter] postNotificationName:@"notificationName" object:nil];

方法二可以传递更多的信息,使用userInfo这个参数,函数如下:

// UserDataObject.h
@property (nonatomic, strong) NSString *property;

// Publisher.m
UserDataObject *userDataObject = [DataObject new];
userDataObject.property = @"This is property value";

NSDictionary *userInfo = [NSDictionary dictionaryWithObject:userDataObject
                                                 forKey:@"additionalData"];

[[NSNotificationCenter defaultCenter] postNotificationName:@"MyNotification"
                                                object:nil
                                              userInfo:userInfo];

注册Notification, 添加observer (看小广告的来了)

上一步有notification发送出来,必须要有相应的observer来响应这些notification, 并对这些notification采取响应的措施。

方法一

// 添加observer
[[NSNotificationCenter defaultCenter] addObserver:self
                                     selector:@selector(eventListenerDidReceiveNotification:)
                                         name:@"MyNotification"
                                       object:nil];

// 对notification采取的动作
- (void)eventListenerDidReceiveNotification:(NSNotification *)notification
{
    if ([[notification name] isEqualToString:@"MyNotification"])
    {
        NSLog(@"Successfully received the notification!");
     
        NSDictionary *userInfo = notification.userInfo;
        UserDataObject *userDataObject = [userInfo objectForKey:@"additionalData"];
     
        // Your response to the notification should be placed here
        NSLog(@"userDataObject.property -> %@", userDataObject.property1);         
  }
}

方法二(采用了block)

self.observer = [[NSNotificationCenter defaultCenter] addObserverForName:@"MyNotification" object:nil queue:nil usingBlock:^(NSNotification *notif) {
     
if ([[notif name] isEqualToString:@"MyNotification"])
{
    NSLog(@"Successfully received the notification!");
         
    NSDictionary *userInfo = notif.userInfo;
    DataObject *dataObject = [userInfo objectForKey:@"additionalData"];
         
    // Your response to the notification should be placed here
    NSLog(@"dataObject.property1 -> %@", dataObject.property1);
}
}];

移除

当observer收到信息并完成操作后,不想再接收信息。

// If registered using addObserver:... method
[[NSNotificationCenter defaultCenter] removeObserver:self];
 
// If registered using addObserverForName:... method
[[NSNotificationCenter defaultCenter] removeObserver:self.observer];
self.observer = nil;
 
// You can also unregister notification types/names using
[[NSNotificationCenter defaultCenter] removeObserver:self name:@"MyNotification" object:nil];

参考文献:

iOS - 使用通知中心(NotificationCenter)進行傳值
Notifications in iOS Part 1 – Broadcasting Events via NSNotificationCenter

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容