4

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

我已经使用此代码发布通知

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

并添加了观察者

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

2 回答 2

6

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

你在调用哪个方法 addObserver:selector:object: in?如果它在 viewWillAppear 中,那么它可能会被多次调用。

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

试试这个:

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

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

于 2011-01-27T13:22:03.377 回答
0

我遇到了同样的问题,并阅读了这个问题,但只能找到一个在项目中的任何位置添加观察者的调用。

在我们的例子中,观察者添加了两次,因为该行所在的方法调用了两次。

确保您单步执行您的代码,中断您的addObserver:selector:name:object调用,以确保您没有对该调用的意外额外执行路径。

于 2012-05-22T01:19:54.533 回答