Unrecognized selector sent With NSNotifications?

The error trace says that an object ThirdViewController recieved the selector updateLabel.

Up vote 2 down vote favorite share g+ share fb share tw.

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 emars3749:b303 -ThirdViewController updateLabel:: unrecognized selector sent to instance 0x685a0a0 2012-01-03 16:14:43.055 emars3749: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 emars6288: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! Objective-c xcode xcode4 xcode4.2 link|improve this question edited Jan 3 at 16:40 asked Jan 3 at 15:20clement109113 62% accept rate.

Which object is receiving the unrecopgnized selector - please add your console contents to the question :) – deanWombourne Jan 3 at 15:39 (And, though I doubt this will help your bug, the object property when you post a notification is for the object which is sending the notification, not for some data. If you want to send data with the notification, use the userInfo parameter). – deanWombourne Jan 3 at 15:40 @deanWombourne : OK thanks.

I've updated the initial post ;-) – clement Jan 3 at 15:49.

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.

OK, many thanks, I begin to understand. The app still crash (I've updated the post... the second edit ^^) – clement Jan 3 at 16:41 1 You're still sending self when you add an observer - you need to register the same class as the method is on, otherwise how would the NotificationCenter know which object to send the notification to? My answer replaces self with otherViewController :) – deanWombourne Jan 3 at 16:44.

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.

1 You're 100% right but @"anyString" is still an object so it should just be very misleading to the receiver of the notification, it shouldn't cause a crash . . .

– deanWombourne Jan 3 at 15:42 @MadhavanRP : Thanks! I've updated the initial post.. I've updated the code but it still crash :'( – clement Jan 3 at 16:44 @clement Ok I've updated my answer, see if that helps you. – MadhavanRP Jan 4 at 4:01.

I cant really gove you an answer,but what I can give you is a way to a solution, that is you have to find the anglde that you relate to or peaks your interest. A good paper is one that people get drawn into because it reaches them ln some way.As for me WW11 to me, I think of the holocaust and the effect it had on the survivors, their families and those who stood by and did nothing until it was too late.

Related Questions