NSNotification in iphone

独自空忆成欢 提交于 2019-12-10 17:36:11

问题


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

回答1:


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




回答2:


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.



来源:https://stackoverflow.com/questions/4816666/nsnotification-in-iphone

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!