用NSNotifications发送的未识别选择器



我在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
...

EDIT 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"]];
}

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

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
...

谢谢你的帮助!

您错误地使用了post - notification方法。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中添加观察者代码。

错误跟踪显示对象ThirdViewController接收了选择器updateLabel:

updateLabel:方法在你的问题看起来不错,但我打赌它在错误的类:)

试试这个:

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

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

最新更新