Bill Nadeau
Bill Nadeau

Reputation: 722

Unrecognized Selector while sending NSNotification

I am attempting to implement a collapsible table view in an iOS application. To do this, I have a gesture recognizer set up in the section headers that will fire NSNotification to the parent controller, which will then refresh the view showing the expanded view.

Everything works up until the parent controller receives its message, which will cause the following error to occur:

'+[MasterViewController receiveTestNotification:]: unrecognized selector sent to class 0xa92a8'

I've looked around on this site and found a few posts relating to this error, but as far as I can tell I am not making those mistakes.

My registration happens in the initialization of the controller and looks like this:

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

The receiver method I want called has this signature:

- (void) receiveTestNotification:(NSNotification *) notification

I send this notification like so, which is in a custom subclass of UIView that I am using as a section header:

[[NSNotificationCenter defaultCenter] postNotificationName:@"RefreshNavigation" object:self];

The examples I have found point to this exact configuration. I am quite certain that the controller isn't being deallocated as it is used shortly thereafter throughout the app.

Any ideas on what I'm doing wrong?

Upvotes: 5

Views: 3520

Answers (1)

jonkroll
jonkroll

Reputation: 15722

Your error message indicates that the notification is being sent to your MasterViewController class, not a MasterViewController instance. You are getting an error because receiveTestNotification: is an instance method, not a class method.

I believe the problem is that you are registering for the notification within the initialize method, which is a class method, so self in that context refers to the class itself, not an instance.

Here is a very similar previous question where the solution was to instead register for the notification in the init method, which is an instance method.

Upvotes: 13

Related Questions