接收的事件子类型是无法识别的选择器

时间:2015-05-26 04:45:37

标签: ios objective-c events event-handling mpmusicplayercontroller

我正在尝试检测控制中心是否按下后退或前进滑动按钮,但是当我运行以下代码时,它会if (receivedEvent.subtype == UIEventSubtypeRemoteControlNextTrack崩溃unrecognized selector sent to instance 0x170259890。我不确定是什么问题以及为什么它不起作用。

- (void)viewDidLoad {
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(handleNowPlayingItemChanged:) name:MPMusicPlayerControllerNowPlayingItemDidChangeNotification object:self.player];
    [self.player beginGeneratingPlaybackNotifications];
}

-(void)handleNowPlayingItemChanged :(UIEvent *)receivedEvent {
    NSLog(@"worked");
    if (receivedEvent.subtype == UIEventSubtypeRemoteControlNextTrack) {
        NSLog(@"next track");
    }
}

1 个答案:

答案 0 :(得分:1)

每当您使用NSNotificationCenter设置通知处理程序时,您必须知道您提供的方法必须以特定方式设置。来自addObserver:selector:name:的文档:

  

选择器,指定接收方发送的消息notificationObserver以通知其通知发布。 notificationSelector指定的方法必须只有一个参数(NSNotification的实例)。

这意味着您的handlePlayingItemChanged:方法必须是:

- (void)handleNowPlayingItemChanged:(NSNotification *)notification {
    NSLog(@"worked");

    MPMusicPlayerController *player = notification.object;
    // get the nowPlayingItem or any other property as needed
}