Does NSNotification work everywhere?
-
20-12-2020 - |
Question
I'm sending a notification using:
[[NSNotificationCenter defaultCenter] postNotificationName:@"historyLoaded" object:jsonReturn];
And receiving the notification using:
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(manageHistory:) name:@"historyLoaded" object:nil];
Then the method in the selector is:
- (void) manageHistory: (NSNotification *) historyData{
NSLog(@"this bit of code was run");
}
For some reaason the notification doesn't get through. Can notifications be send and received from anywhere in the app?
Solution
The object
parameter in postNotification
should be filled with an object which is "sending" the notification, or nil if the sender is not necessarily specified.
If you want to pass some information along, you should use postNotificationName:object:userInfo
and put the information in userInfo
dictionary instead.
OTHER TIPS
[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(manageHistory) name:@"historyLoaded" object:nil];
[[NSNotificationCenter defaultCenter] postNotificationName:@"historyLoaded"
object:nil userInfo:jsonReturn];
- (void) manageHistory: (NSNotification *) historyData{
NSDictionary* _dict = historyData.userInfo;
NSLog(@"Your information embedded to dictiuonary obj %@",_dict);
}
NOTE : Make sure your historyData should be a dictionary object in postNotificationName
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow