Reputation: 4266
I'm using NSNotifications in a ViewController to update a UILabel in another ViewController by pressing a button :
- (void)connectionDidFinishLoading:(NSURLConnection *)connection {
...
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(updateLabel:)
name:@"LABELUPDATENOTIFICATION" object:nil];
[[NSNotificationCenter defaultCenter] postNotificationName:@"LABELUPDATENOTIFICATION" object:@"test"];
...
And the code that receipt the String (@"test") :
- (void)updateLabel:(NSNotification*)notification
{
NSString *updatedText = (NSString*)[notification object];
[details setText:updatedText];
}
It gives me an arror when I press the button (when the first code is executed) and the second code is highlited with an error: "unrecognized selector sent to instance"
Thanks to help me, I don't really understand notifications...
A second question could be how send a second notification to update a second UILabel in the same Controller...
MANY THANKS!
EDIT: HERE'S THE ERROR TRACE:
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];
and in the other class :
- (void)updateLabel:(NSNotification*)notification
{
NSDictionary *dictionnary = (NSDictionary*)[notification object];
[details setText:[dictionnary objectForKey:@"adetails"]];
[recapitulatif setText:[dictionnary objectForKey:@"recapitulatif"]];
}
I have two errors ; a cute SIGABRT and :
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
...
THANKS for your help !!!
Upvotes: 2
Views: 4292
Reputation: 2830
You are using the post notification method incorrectly. the object:
argument is the object that posts the notification. You've given that object as @"test"
. This string object should not be posting any notifications. What you need to be doing is something along the lines of
[[NSNotificationCenter defaultCenter] postNotificationName:@"LABELUPDATENOTIFICATION" object:self userInfo:aDictionary];
This userInfo
should be an NSDictionary
, and is how you pass objects through a notification. You can use the same notification to pass the text for both the labels. Just construct the dictionary with the strings.
aDictionary=[NSDictionary dictionaryWithObjectsAndKeys:@"test1",@"label1",@"test2",@"label2",nil];
EDIT Right, you still seem to have a misunderstanding of how notifications work. Let's say you have two view controllers VC1 and VC2. If VC1 is going to post a notification with the values for the labels(which are present in VC2), it should not be the observer. You've posted it correctly but your statement
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(updateLabel:)
name:@"LABELUPDATENOTIFICATION" object:nil];
should not be in VC1. Or rather, as @dean Wombourne said, you should add the other View Controller(VC2) as the observer. The reason for your crash is you are adding VC1 as observer and it does not implement updateLabel:
. I would guess you donn't have a reference for VC2 object. So just cut and paste the add observer code in viewDidLoad
of VC2.
Upvotes: 3
Reputation: 38475
The error trace says that an object ThirdViewController
recieved the selector updateLabel:
The updateLabel:
method in your question looks OK but I bet it's in the wrong class :)
Try this instead :
[[NSNotificationCenter defaultCenter] addObserver:otherViewController selector:@selector(updateLabel:) name:@"LABELUPDATENOTIFICATION" object:nil];
In your question you are asking your notification to be sent to self
, which doesn't have an updateLabel:
method. You need to the notificationCenter which object wants to receive the notification.
Upvotes: 1