我试图检测是否在控制中心按下了后退或前进滑动按钮,但是当我运行以下代码时,它会崩溃 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 bynotificationSelector
must have one and only one argument (an instance ofNSNotification
).
这意味着您的 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/