美文网首页不明觉厉iOS程序员iOS进阶相关
通过NSNotificationCenter传递信息

通过NSNotificationCenter传递信息

作者: 陈旭冉 | 来源:发表于2014-05-17 23:10 被阅读817次

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

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

    NotificationCenterNotificationCenter

    雅一点的原理如下:

    学术一点学术一点

    先讲讲俗的原理

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

    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

    相关文章

      网友评论

        本文标题:通过NSNotificationCenter传递信息

        本文链接:https://www.haomeiwen.com/subject/vwtdtttx.html