【问题标题】:NSNotification removeObserver problemNSNotification removeObserver 问题
【发布时间】:2011-05-11 09:12:18
【问题描述】:

我要么是大脑受损,要么是我对 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。

那么任何想法为什么它确实告诉我我没有首先添加和观察者?

【问题讨论】:

    标签: iphone objective-c ios nsnotificationcenter


    【解决方案1】:

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

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

    【讨论】:

    • 谢谢,我确实是脑残了 :-) 呵呵,至少今天是这样。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2010-12-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多