iOS:NSNotification无法识别的选择器错误

时间:2013-04-05 05:02:59

标签: ios objective-c nsnotificationcenter

NSNotification *notification = [NSNotification notificationWithName:@"locationObtained" object:self];
[[NSNotificationCenter defaultCenter]postNotification:notification];

我得到[LocavoreRetroFirstViewController startServices]: unrecognized selector sent to instance 0x74c3070

- (void)listenForLocationCompletion{
    [[NSNotificationCenter defaultCenter]addObserver:self
                                            selector:@selector(startServices)
                                                name:@"locationObtained"
                                              object:nil];
}

- (void)startServices:(NSNotification *)notification{

}

我收到此错误的原因是什么?

4 个答案:

答案 0 :(得分:2)

更改为:

[[NSNotificationCenter defaultCenter]addObserver:self
                                            selector:@selector(startServices:)
                                                name:@"locationObtained"
                                              object:nil];
}

您的选择器startServices在结束时错过了“:”

答案 1 :(得分:1)

您有一个LocavoreRetroFirstViewController对象已注册locationObtained通知,并且此后已被取消分配。

在LocavoreRetroFirstViewController的实现文件中添加:

-(void) dealloc {
    [[NSNotificationCenter defaultCenter] removeObserver:self];
}

答案 2 :(得分:0)

您是否检查过是否有名为startServices的方法?发布通知后,LocavoreRetroFirstViewController的对象是否有效?请发布通知的观察者代码,以便我可以更多地了解。

答案 3 :(得分:0)

您忘记了选择器名称中的分号。

应为@selector(startServices:)