无法识别的选择器发送到实例

时间:2013-11-18 13:53:02

标签: ios ios7 nsnotificationcenter

我在选择通知方法时遇到问题。

在初学者中我已经明确了这个:

NSNotificationCenter *nc = [NSNotificationCenter defaultCenter];
[nc addObserver:self selector:@selector(stopSyncIndicator:) name:IOS_STOP_SYNC_INDICATOR object:nil];

虽然该方法在标题和相同的实现中都有定义:

-(void)stopSyncIndicator
{
    [indicator stopAnimating];
}

但是,当其他班级发布此通知时:

NSNotification *note = [NSNotification notificationWithName:IOS_STOP_SYNC_INDICATOR object:nil];
[[NSNotificationCenter defaultCenter] postNotification:note];

地狱崩溃了:

[FTRecordViewController stopSyncIndicator:]: unrecognized selector sent to instance 0x8d3bc00
2013-11-18 13:47:06.994 [1835:70b] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[FTRecordViewController stopSyncIndicator:]: unrecognized selector sent to instance 0x8d3bc00'
*** First throw call stack:
(
    0   CoreFoundation                      0x01bf75e4 __exceptionPreprocess + 180
    1   libobjc.A.dylib                     0x018f88b6 objc_exception_throw + 44
    2   CoreFoundation                      0x01c94903 -[NSObject(NSObject) doesNotRecognizeSelector:] + 275

知道这里发生了什么吗?

2 个答案:

答案 0 :(得分:6)

你的选择器有:表示它会接受一个参数,你的实现不是

或者

@selector(stopSyncIndicator) //no :

-(void)stopSyncIndicator:(NSNotification *)notification //accept argument 

会解决这个问题

答案 1 :(得分:2)

你告诉观察者的选择器有一个参数:

[nc addObserver:self selector:@selector(stopSyncIndicator:) name:IOS_STOP_SYNC_INDICATOR object:nil];`<br/>

并且您的选择器没有参数:

-(void)stopSyncIndicator

要解决此问题,请从:移除selector:@selector(stopSyncIndicator:)或将您的方法签名设置为:

-(void)stopSyncIndicator:(NSNotification *)notification