简而言之,我在 ClassA
(在 viewDidLoad
中)注册了以下 NSNotification
监听器:
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(playSong) name:@"playNotification" object:nil];
我在 ClassA.h
中声明了选择器:
- (void)playSong:(NSNotification *) notification;
实现如下:
- (void)playSong:(NSNotification *) notification {
NSString *theTitle = [notification object];
NSLog(@"Play stuff", theTitle);
}
在 ClassB
中(在 tableView:didSelectRowAtIndexPath:
方法中)我有:
NSInteger row = [indexPath row];
NSString *stuff = [playlistArray objectAtIndex:row];
[[NSNotificationCenter defaultCenter] postNotificationName:@"playNotification" object:stuff];
这一切都以一条错误信息告终:
"unrecognized selector sent to instance"
在 playSong
方法被调用之前。
有人可以帮我解决这个问题吗?从一个 Controller 向另一个 Controller 发送通知时我忘记了什么?
最佳答案
你的 @selector
需要一个 :
字符,如果它要接受一个参数:
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(playSong:) name:@"playNotification" object:nil];
ClassA
的实例不响应playSong
选择器,但它们确实响应playSong:
选择器。
关于iphone 通知结果为 "unrecognized selector sent to instance...",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4523357/