Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

iphone notification results in "unrecognized selector sent to instance..."

To make it short, I'm registering the following NSNotification listener in ClassA (in viewDidLoad):

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

I have the selector declared in ClassA.h:

- (void)playSong:(NSNotification *) notification;

And implementation goes as follows:

- (void)playSong:(NSNotification *) notification {
    NSString *theTitle = [notification object]; 
    NSLog(@"Play stuff", theTitle);
}

In ClassB (in the tableView:didSelectRowAtIndexPath: method) I have:

NSInteger row = [indexPath row];
NSString *stuff = [playlistArray objectAtIndex:row];
[[NSNotificationCenter defaultCenter] postNotificationName:@"playNotification" object:stuff];

It all end up with an error message saying:

"unrecognized selector sent to instance"

before the playSong method is invoked.

Can anybody please help me out here? What am I forgetting when posting a notification from one controller to another?

like image 304
esbenr Avatar asked Dec 23 '10 23:12

esbenr


1 Answers

Your @selector needs a : character if it is to take an argument:

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

Instances of ClassA do not respond to the playSong selector, but they do respond to the playSong: selector.

like image 85
Jacob Relkin Avatar answered Oct 24 '22 06:10

Jacob Relkin