在iphone中的NSNotification

Ami*_*wal 4 iphone nsnotificationcenter

我正在将NSSNotifcation发送到iPhone应用程序中的另一个视图控制器,但其观察者方法被告知两次如何可能任何人指导我

我已使用此代码发布通知

[[NSNotificationCenter defaultCenter] postNotificationName:@"updateStatusOnFacebook" object:nil userInfo:nil];
Run Code Online (Sandbox Code Playgroud)

并增加了观察员

[[NSNotificationCenter defaultCenter]   addObserver:self  selector:@selector(postToWall)                name:@"updateStatusOnFacebook"  object:nil];
Run Code Online (Sandbox Code Playgroud)

dea*_*rne 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];
Run Code Online (Sandbox Code Playgroud)

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