ios - 收到的事件子类型是无法识别的选择器

标签 ios objective-c events event-handling mpmusicplayercontroller

我试图检测是否在控制中心按下了后退或前进滑动按钮,但是当我运行以下代码时,它会崩溃 if (receivedEvent.subtype == UIEventSubtypeRemoteControlNextTrack无法识别的选择器发送到实例 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");
    }
}

最佳答案

任何时候你使用 NSNotificationCenter 设置一个通知处理程序,你必须知道你提供的方法必须以特定的方式设置。来自 addObserver:selector:name::

的文档

Selector that specifies the message the receiver sends notificationObserver to notify it of the notification posting. The method specified by notificationSelector must have one and only one argument (an instance of NSNotification).

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

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

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

关于ios - 收到的事件子类型是无法识别的选择器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30449662/

相关文章:

ios - 使用Twitter SDK发送推文失败,但我不知道为什么

iphone - 如何在 3G 手机上加快第一次 View 转换的速度?

ios - 使用 BringSubviewToFront 时图像回弹

ios - 缩放时 UIScrollView subview 居中

iphone - 如果我释放,我将无法访问,如果我保留,我将泄漏

events - knockout : prevent event bubbling for elements with no handler

javascript - 如何将一个事件监听器附加到多个对象?

ios - MFMailComposeViewController 的自定义 UINavigationBar

ios - 如何使用 Swift 4 在 ARKit 中旋转虚拟对象

c++ - 用 C++ 编写一个非常简单的事件类