Searched and searched, but couldn't find the answer. Maybe it's related to Cocos2d? Probably not - it's likely just my bad coding skills!
I'm using NSNotificationCenter to let me know when a button is pressed in my app. Maybe not the best way to use the service, but I'm experimenting trying to get it to work.
In my Layer, I have the following code:
#define kLeftButtonNotify @"Left Button"
And in the custom init method for the Layer:
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(onLeftPress:) name:kLeftButtonNotify object:sprite];
'sprite' is an AtlasSprite that has been sub-classed to allow it to do some extra things when it's touched. One of the things is this:
[[NSNotificationCenter defaultCenter] postNotificationName:notifyName object:self];
'notifyName' is retained in the sprite itself on creation, being passed kLeftButtonNotify.
The app works fine. Notifications are posted and received properly.
The problem comes in removing the Layer as an observer. When I change views using 'replaceScene', then return to this view using replaceScene once again, the notification is received twice, as if it was not removed as an observer.
Each time I change and return to this scene with this layer, it keeps increasing the number of notifications I get.
In the dealloc method of the Layer:
[[NSNotificationCenter defaultCenter] removeObserver:self];
I've also tried:
AtlasSpriteCustom *sprite = (AtlasSpriteCustom *)[self getChildByTag:kLeftButtonTag];
[[NSNotificationCenter defaultCenter] removeObserver:self name:kLeftButtonNotify object:sprite];
...but it doesn't make any difference.
Thanks for any help!