4

我对 NSNotificationCenter 有疑问。现在它崩溃了,但几天前,当我添加通知时,它工作正常。在这期间,我添加了一些与此无关的代码。

我有大约 10x10 的瓷砖。每个图块在创建后立即将自己添加为观察者:

[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(playerJumped) name:@"TestNot" object:nil];

在我的播放器类中,每次跳转结束时,我都会使用以下代码发布通知:

if (self.postNotifications == YES) {
    //Also post the notification for all the Tiles.
    [[NSNotificationCenter defaultCenter] postNotificationName:@"TestNot" object:self];
}

如果我在图块中使用 NSLog(),我可以看到大约 3 或 4 个图块收到通知。之后,应用程序因 EXC_BAD_ACCESS 而崩溃。它说objc_msgSend() selector name: playerJumped。但我不明白为什么。我看到它适用于第一个而不是崩溃。我的错误是什么?你能帮我么!桑德罗

编辑:有问题吗,因为大约 100 个对象收到了通知?

4

2 回答 2

10

我自己也有同样的问题。将此添加到课程中解决了问题

- (void) dealloc 
{

  [[NSNotificationCenter defaultCenter] removeObserver:self];

}
于 2012-10-18T13:47:04.340 回答
9

您的 tile 对象已被释放,但仍向 notificationCenter 注册以接收通知。如果这不是您所期望的,请尝试在磁贴的 -dealloc 方法上添加断点。

于 2010-09-26T11:46:06.550 回答