iphone - NSNotification removeObserver问题

标签 iphone objective-c ios nsnotificationcenter

要么我脑残要么我对 NSNotificationCenter 缺乏一些了解

问题是,如果我创建了一个观察者,并且在下一行将尝试像这样删除它:

[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(removeAllVisibleMapViews) name:@"ClearVisibleMaps" object:nil];
[[NSNotificationCenter defaultCenter] removeObserver:self forKeyPath:@"ClearVisibleMaps"];

我明白了

*** Terminating app due to uncaught exception 'NSRangeException', reason: 'Cannot remove an observer <MyApp 0x592db70> for the key path "ClearVisibleMaps" from <NSNotificationCenter 0x4e0fbb0> because it is not registered as an observer.'

我逐行添加和删除观察者只是为了说明一点。在我的代码中,我将在 dealloc 中使用 remove。

那么为什么它确实告诉我我一开始没有添加和观察者有什么想法吗?

最佳答案

您正在删除键路径的观察者,而不是通知名称。删除应该是这样的:

[[NSNotificationCenter defaultCenter] removeObserver:self
                                                name:@"ClearVisibleMaps"
                                              object:nil];

关于iphone - NSNotification removeObserver问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5961828/

相关文章:

iphone - Xcode5 - 无法添加我的 Apple ID

ios - iOS 中字体 Glyph Ascender 和 Descender 之间的关系是什么?

ios - 变量初始化错误

objective-c - 用灰色突出显示 UIImageView

ios - 在哪里插入[tableView reloadData]

iphone - Monotouch - 从我的应用程序将事件添加到 iPhone 日历

ios - 是否有任何库可以在 iPhone 中使用 AVCaptureSession 获取 vorbis 格式的音频数据?

objective-c - 当应用程序未激活时 iOS 后台下载

objective-c - 无法调用 xcode 中 "+(void)"函数中的函数

iphone - 为什么字体大小(和其他元素)在移动设备上显得这么小