I am building a program that utilises NSNotification
as I want to be able to pass information through from another class that is going to affect the value of variables in a different class.
So, I have set up the following:
categories.m
class:
In viewDidLoad
:
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(updateTheScore:)name:@"TheScore" object:nil];
in the same class, with my updateTheScore
function:
- (void)updateTheScore:(NSNotification *)notification
{
NSLog(@"Notification Received. The value of the score is currently %d", self.mainScreen.currentScore);
[[NSNotificationCenter defaultCenter]removeObserver:self];
}
In mainScreen.m
:
self.currentScore++;
[[NSNotificationCenter defaultCenter]postNotificationName:@"TheScore" object:self];
The score in a usual instance would update from 0 to 1.
The program will call the notification
correctly, as I can see my NSLog
being performed. However, the value of the variable is not passing through, and this is where I am stuck.
Can anyone please think of a solution as to why my variable value is not passing through?
To clarify, if I do an NSLog right before the postNotificationName
line to show me the value of self.currentScore;
this returns 1, as expected. In the updateTheScore
function, it returns 0
.
Thanks in advance to everyone.