【问题标题】:unrecognized selector sent With NSNotifications使用 NSNotifications 发送无法识别的选择器
【发布时间】:2012-01-03 15:20:27
【问题描述】:

我在 ViewController 中使用 NSNotifications 通过按下按钮来更新另一个 ViewController 中的 UILabel:

- (void)connectionDidFinishLoading:(NSURLConnection *)connection {
...
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(updateLabel:)
         name:@"LABELUPDATENOTIFICATION" object:nil];
        [[NSNotificationCenter defaultCenter] postNotificationName:@"LABELUPDATENOTIFICATION" object:@"test"]; 
...

以及接收字符串的代码(@“test”):

- (void)updateLabel:(NSNotification*)notification
{
    NSString *updatedText = (NSString*)[notification object];
    [details setText:updatedText];
}

当我按下按钮(执行第一个代码时)和第二个按钮时,它给了我一个错误 代码高亮显示错误:“无法识别的选择器已发送到实例”

感谢帮助,我不太懂通知...

第二个问题可能是如何发送第二个通知来更新同一控制器中的第二个 UILabel...

非常感谢!

编辑: 这是错误跟踪:

2012-01-03 16:14:43.054 emars[3749:b303] -[ThirdViewController updateLabel:]: unrecognized selector sent to instance 0x685a0a0
2012-01-03 16:14:43.055 emars[3749:b303] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[ThirdViewController updateLabel:]: unrecognized selector sent to instance 0x685a0a0'
*** Call stack at first throw:
(
    0   CoreFoundation                      0x00fb45a9 __exceptionPreprocess + 185
    1   libobjc.A.dylib                     0x01108313 objc_exception_throw + 44
    2   CoreFoundation                      0x00fb60bb -[NSObject(NSObject) doesNotRecognizeSelector:] + 187
    3   CoreFoundation                      0x00f25966 ___forwarding___ + 966
    4   CoreFoundation                      0x00f25522 _CF_forwarding_prep_0 + 50
...

编辑 2:

NSDictionary *myDictionnary = [NSDictionary dictionaryWithObjectsAndKeys:@"details",@"details de l'installation",@"recapitulatif",@"recapitulatif de l'installe",nil];
        [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(updateLabel:)
         name:@"LABELUPDATENOTIFICATION" object:nil];
        [[NSNotificationCenter defaultCenter] postNotificationName:@"LABELUPDATENOTIFICATION" object:self userInfo:myDictionnary];

在另一个班级:

- (void)updateLabel:(NSNotification*)notification
{
    NSDictionary *dictionnary = (NSDictionary*)[notification object];
    [details setText:[dictionnary objectForKey:@"adetails"]];
    [recapitulatif setText:[dictionnary objectForKey:@"recapitulatif"]];
}

我有两个错误;一个可爱的 SIGABRT 和:

unrecognized selector sent to instance 0x683d630
2012-01-03 17:37:21.783 emars[6288:b303] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[ThirdViewController updateLabel:]: unrecognized selector sent to instance 0x683d630'
*** Call stack at first throw:
(
    0   CoreFoundation                      0x00fb45a9 __exceptionPreprocess + 185
    1   libobjc.A.dylib                     0x01108313 objc_exception_throw + 44
    2   CoreFoundation                      0x00fb60bb -[NSObject(NSObject) doesNotRecognizeSelector:] + 187
    3   CoreFoundation                      0x00f25966 ___forwarding___ + 966
...

感谢您的帮助!!!

【问题讨论】:

  • 哪个对象正在接收无法识别的选择器 - 请将您的控制台内容添加到问题中:)
  • (而且,虽然我怀疑这会对您的错误有所帮助,但当您发布通知时的 object 属性是针对 sending 通知的对象,而不是某些数据。如果你想通过通知发送数据,请使用 userInfo 参数)。
  • @deanWombourne:好的,谢谢。我已经更新了最初的帖子;-)

标签: objective-c xcode xcode4 xcode4.2


【解决方案1】:

您错误地使用了发布通知方法。 object: 参数是发布通知的对象。您已将该对象指定为@"test"。此字符串对象不应发布任何通知。您需要做的是类似于

[[NSNotificationCenter defaultCenter] postNotificationName:@"LABELUPDATENOTIFICATION" object:self userInfo:aDictionary];

这个userInfo 应该是一个NSDictionary,这是您通过通知传递对象的方式。您可以使用相同的通知来传递两个标签的文本。只需用字符串构造字典即可。

aDictionary=[NSDictionary dictionaryWithObjectsAndKeys:@"test1",@"label1",@"test2",@"label2",nil];

编辑 是的,您似乎仍然对通知的工作方式存在误解。假设您有两个视图控制器 VC1 和 VC2。如果 VC1 要发布带有标签值的通知(存在于 VC2 中),它不应该是观察者。您已正确发布,但您的声明

[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(updateLabel:)
     name:@"LABELUPDATENOTIFICATION" object:nil];

不应在 VC1 中。或者更确切地说,正如@dean Wombourne 所说,您应该添加另一个视图控制器(VC2)作为观察者。崩溃的原因是您将 VC1 添加为观察者,但它没有实现 updateLabel:。我猜你没有 VC2 对象的参考。所以只需在VC2的viewDidLoad中剪切并粘贴添加观察者代码即可。

【讨论】:

  • 你是 100% 正确的,但 @"anyString" 仍然是一个对象,所以它应该对通知的接收者非常误导,它不应该导致崩溃。 . .
  • @MadhavanRP:谢谢!!!我已经更新了最初的帖子。我已经更新了代码,但它仍然崩溃:'(
  • 我有不同的配置,但答案让我遇到了问题:我有 object:nil 而不是 object:self
【解决方案2】:

错误跟踪表明对象ThirdViewController 收到了选择器updateLabel:

您问题中的updateLabel: 方法看起来不错,但我敢打赌它属于错误的类:)

试试这个:

[[NSNotificationCenter defaultCenter] addObserver:otherViewController selector:@selector(updateLabel:) name:@"LABELUPDATENOTIFICATION" object:nil];

在您的问题中,您要求将通知发送到self,它没有updateLabel: 方法。您需要通知中心哪个对象要接收通知。

【讨论】:

  • 好的,非常感谢,我开始明白了。该应用程序仍然崩溃(我已经更新了帖子......第二次编辑^^)
  • 当您添加观察者时,您仍然在发送self - 您需要注册与方法相同的类,否则 NotificationCenter 将如何知道将通知发送到哪个对象?我的回答将self 替换为otherViewController :)
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多