Send and receive messages through NSNotificationCenter in Objective-C?

后端 未结 6 1502
感动是毒
感动是毒 2020-11-21 22:40

I am attempting to send and receive messages through NSNotificationCenter in Objective-C. However, I haven\'t been able to find any examples on how to do this.

6条回答
  •  無奈伤痛
    2020-11-21 23:00

    There is also the possibility of using blocks:

    NSOperationQueue *mainQueue = [NSOperationQueue mainQueue];
    [[NSNotificationCenter defaultCenter] 
         addObserverForName:@"notificationName" 
         object:nil
         queue:mainQueue
         usingBlock:^(NSNotification *notification)
         {
              NSLog(@"Notification received!");
              NSDictionary *userInfo = notification.userInfo;
    
              // ...
         }];
    

    Apple's documentation

提交回复
热议问题