Providing Notification to Another Class Using NSNotificationCenter

So my goal is to send a notification to another class using NSNotificationCenter , I also want to pass an object with a notification to another class , how do I do this?

+3
source share
2 answers

You must first register a notification name

 [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(startLocating:) name:@"ForceUpdateLocation" object:nil]; // don't forget the ":" 

Then send a notification with the parameter dictionary

 [[NSNotificationCenter defaultCenter] postNotificationName:@"ForceUpdateLocation" object:self userInfo:[NSDictionary dictionaryWithObject:@"1,2,3,4,5" forKey:@"categories_ids"]]; 

and the method will be

 - (void)startLocating:(NSNotification *)notification { NSDictionary *dict = [notification userInfo]; } 
+7
source

Just call on any method of sending notifications, as described here , for example:

To post a notification:

 -(void)postNotificationName:(NSString *)notificationName object:(id)notificationSender userInfo:(NSDictionary *)userInfo; 

where userInfo is a dictionary containing useful objects.

On the other hand, register for notifications:

 -(void)addObserver:(id)notificationObserver selector:(SEL)notificationSelector name:(NSString *)notificationName object:(id)notificationSender; 

You can also check out Apple Notification Programming Themes .

0
source

Source: https://habr.com/ru/post/1413516/


All Articles