Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

NSNotification being raised multiple times

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];
}
like image 878
A for Alpha Avatar asked Feb 02 '11 09:02

A for Alpha


2 Answers

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.

like image 91
Jilouc Avatar answered Nov 06 '22 23:11

Jilouc


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....

like image 1
A for Alpha Avatar answered Nov 07 '22 00:11

A for Alpha