我要么是大脑受损,要么是我对 NSNotificationCenter 缺乏一些了解
问题是,如果我创建一个观察者,并在下一行中尝试删除它,如下所示:
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(removeAllVisibleMapViews) name:@"ClearVisibleMaps" object:nil];
[[NSNotificationCenter defaultCenter] removeObserver:self forKeyPath:@"ClearVisibleMaps"];
I get
*** 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 中使用删除。
那么,为什么它告诉我我一开始就没有添加观察者,有什么想法吗?
您要删除键路径的观察者,而不是通知名称的观察者。删除应该是这样的:
[[NSNotificationCenter defaultCenter] removeObserver:self
name:@"ClearVisibleMaps"
object:nil];
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)