简而言之,我正在注册以下内容 NSNotification 听众进来 ClassA (在 viewDidLoad):

[[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 调用方法。

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

有帮助吗?

解决方案

您的 @selector 需要一个 : 角色是否要论证:

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

实例 ClassA不是 回应 playSong 选择器,但他们 回应 playSong: 选择器。

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top