Search code examples
iphonensnotification

nsnotificationcenter method fired more than once


i have a viewcontroller .In it there is a nsnotification observer in it. i am posting the notification from another viewcontroller.but the nsnotification observers selector get fired two or sometimes three times. My question is that when i use [view removeFromSuperview]; to remove this viewcontrollers view ,is the notification observer removed? I have given this method at the dealloc method of the viewcontroller class

- (void)dealloc {
    [super dealloc];
    [[NSNotificationCenter defaultCenter] removeObserver:self];

}

Solution

  • No.

    that method will be called when the viewcontrollers retain count becomes 0

    You should add another method that will be called when the view is removed from the other viewcontroller and call

    [[NSNotificationCenter defaultCenter] removeObserver:self];
    

    For the issue that the selector is called multiple times, I would need to see more code - make sure that the line of code thats posting the notification isnt being called multiple times