开发者

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];


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 :)


I had the same problem crop up, and read this question, but could only find the one call to add the observer anywhere in the project.

In our case, the observer was being added twice because the method the line was in was being called twice.

Make sure you step through your code, breaking on your addObserver:selector:name:object call, to ensure that you don't have an unexpected extra execution path to that call.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜