我正在尝试检测控制中心是否按下后退或前进滑动按钮,但是当我运行以下代码时,它会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");
}
}
答案 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
}