要么我脑残要么我对 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/