iphone 通知结果为 "unrecognized selector sent to instance..."

标签 iphone objective-c ios nsnotificationcenter

简而言之,我在 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/

相关文章:

ios - 如何在解析中使用手机号码登录,但用户应该通过短信或调用该号码获取验证码?

ios - 在 iOS 中访问用户联系人的问题

IOS Sprite 套件,旋转一个SKSpriteNode

iphone - MK注释: Reading a tag for MKAnnotation

iphone - iOS 中图像查看器应用程序的策略

iphone - iPhone UIGetScreenImage 的代码示例

ios - 标签的 UIButton 文本不可见

ios - uikit 方法是否保证在主线程上运行?

ios - 使用 nib 文件开始/结束外观转换的不平衡调用

ios - 更多选项卡、行图像色调 (Xcode 9 + Swift 4)