通過(guò)NSNotificationCenter傳遞信息

iOS通過(guò)NSNotificationCenter來(lái)傳遞通告(Notification)的原理非常簡(jiǎn)單。

俗一點(diǎn)的原理如下:這個(gè)電線桿就是一個(gè)NotificationCenter. 誰(shuí)都可以來(lái)發(fā)通告(Notification),誰(shuí)都可以來(lái)看通告(Notification)。

NotificationCenter
NotificationCenter

雅一點(diǎn)的原理如下:

學(xué)術(shù)一點(diǎn)
學(xué)術(shù)一點(diǎn)

先講講俗的原理

電線桿上的牛皮癬廣告包含了至少三個(gè)參與方:

  1. 電線桿 (充當(dāng)NSNotificationCenter的角色)
  2. 有祖?zhèn)骼现嗅t(yī)的廣告張貼者 (充當(dāng)Poster的角色)
  3. 有祖?zhèn)髋Fぐ_的路人甲乙丙丁 (充當(dāng)Observer的角色)

發(fā)送notification(貼小廣告的來(lái)了)

方法一不用傳遞更多的信息,函數(shù)如下:

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

方法二可以傳遞更多的信息,使用userInfo這個(gè)參數(shù),函數(shù)如下:

// 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];

注冊(cè)Notification, 添加observer (看小廣告的來(lái)了)

上一步有notification發(fā)送出來(lái),必須要有相應(yīng)的observer來(lái)響應(yīng)這些notification, 并對(duì)這些notification采取響應(yīng)的措施。

方法一

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

// 對(duì)notification采取的動(dòng)作
- (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);
}
}];

移除

當(dāng)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];

參考文獻(xiàn):

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

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

推薦閱讀更多精彩內(nèi)容