【问题标题】:iphone notification results in "unrecognized selector sent to instance..."iphone通知导致“无法识别的选择器发送到实例......”
【发布时间】:2010-12-23 23:58:20
【问题描述】:

简而言之,我在ClassAviewDidLoad)中注册了以下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];

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

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

在调用playSong 方法之前。

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

【问题讨论】:

    标签: iphone objective-c ios nsnotificationcenter


    【解决方案1】:

    您的@selector 需要一个: 字符才能接受参数:

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

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

    【讨论】:

    • 成功了。谢谢伙计,这么小的细节:-) 最后一个问题:如果我想指定我希望选择器采用哪种类型的对象,这样做的语法是什么? (object:nil) 其中 nil 应该是定义。
    • @esbenr 当然,您可以在object 参数中传入任何您喜欢的内容。
    • 就我而言,症状相同,但原因恰恰相反。我的selector 上有一个:,用于没有参数的方法。在我的情况下,删除 : 修复了它。
    猜你喜欢
    • 2017-01-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多