NSNotification being raised multiple times
Asked Answered
F

2

10

i am developing a e-book reader and i had encountered the following issue. i am using an IBAction method to post a NSNotification which inturn invokes a action method once a button is tapped. It is working absolutely fine for the first time... The IBAction method has to be called evry time i tap on the button.After completing this action, i need to go to my home page and then come back and click the button again. This is where i am facing the problem. The Method is getting called more than once from the second time and the number only keeps increasing.. Please help me with this....

//IBAction Method
if (sender.tag == PenToolAction) {

    GLogInfo(@"Pen tool tapped");
    [self hideSelf];
    [[NSNotificationCenter defaultCenter] postNotificationName:PenToolActionInitialized object:nil];
}
Fike answered 2/2, 2011 at 9:42 Comment(2)
Where did you put the [[NSNotificationCenter defaultCenter] addObserver:...] call?Tillie
i have put that in the ViewDidLoad method of the receivers class.Fike
T
34

If you add the notification observer in a method called multiple times, you will receive the notification multiple times too.

Try to either put the [[NSNotificationCenter defaultCenter] addObserver:...] in the init method instead or call [[NSNotificationCenter defaultCenter] removeObserver:self] right before.

Tillie answered 2/2, 2011 at 16:14 Comment(1)
I wish apple would document that better. And I wish there was better public API so you could determine if an object has already registered to a given notification.Papiamento
F
1

Thanku you so much for ur support.. I found the solution for my question... The main culprit here in my code is the issue of memory management... I'v used x-code analyzer to check the leaks and found that i forgot to release a main variable..... I'v released the variable and now my code is working fine....

Fike answered 4/2, 2011 at 5:47 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.