14

简而言之,我在(in ) 中注册了以下NSNotification侦听器:ClassAviewDidLoad

[[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];

这一切都以一条错误消息告终:

“无法识别的选择器发送到实例”

playSong调用方法之前。

有人可以帮我吗?从一个控制器向另一个控制器发布通知时我忘记了什么?

4

1 回答 1

42

如果要进行论证,您@selector需要一个角色::

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

的实例ClassA不响应选择器,但它们确实响应选择器。playSongplaySong:

于 2010-12-24T00:02:20.323 回答