【发布时间】: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