我将NSSnotifcation发送到iPhone应用中的另一个视图控制器,但其观察者方法被通知两次,任何可能的人都可以指导我

我使用此代码发布通知

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

并添加了观察者

[[NSNotificationCenter defaultCenter]   addObserver:self  selector:@selector(postToWall)                name:@"updateStatusOnFacebook"  object:nil];
有帮助吗?

解决方案

您是否添加了两次观察者?

您调用哪种方法addobserver:selector:object:in?如果它在ViewWillAppear中,则可以多次调用。

您的方法将被称为与添加观察者相同的次数。

尝试这个:

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

另一个原因是您可能只是发送两次通知:)

其他提示

我遇到了同样的问题,并阅读了这个问题,但只能找到一个呼叫,可以在项目中的任何地方添加观察者。

在我们的情况下,观察者 曾是 被添加了两次,因为 方法 这条线正在 两次。

确保您浏览代码,打破您的代码 addObserver:selector:name:object 致电,以确保您没有意外的额外执行路径。

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top