Search code examples
iphonensnotificationcenter

NSNotification in iphone


i am sending NSSNotifcation to another view controller in iPhone app but its observer method getting notified two times how its possible can any one guide me

i have use this code to post notification

[[NSNotificationCenter defaultCenter] postNotificationName:@"updateStatusOnFacebook" object:nil userInfo:nil];

and added observer

[[NSNotificationCenter defaultCenter]   addObserver:self  selector:@selector(postToWall)                name:@"updateStatusOnFacebook"  object:nil];

Solution

  • Have you added the observer twice?

    Which method are you calling addObserver:selector:object: in? If it's in viewWillAppear then this might be called more than once.

    Your method will be called the same number of times that you have added an observer.

    Try this:

    [[NSNotificationCenter defaultCenter] removeObserver:self name:@"updateStatusOnFacebook" object:nil];
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(postToWall) name:@"updateStatusOnFacebook" object:nil];
    

    The other reason is that you might just be sending the notification twice :)