MPRemoteCommandCenter多次调用处理程序块,并导致对选择器方法的不必要调用.
这是代码片段:
MPRemoteCommandCenter *commandCenter = [MPRemoteCommandCenter sharedCommandCenter]; [commandCenter.nextTrackCommand addTargetWithHandler:^MPRemoteCommandHandlerStatus(MPRemoteCommandEvent * _Nonnull event) { NSLog(@"NEXTTTTTT"); return MPRemoteCommandHandlerStatusSuccess; }]; [commandCenter.prevIoUsTrackCommand addTargetWithHandler:^MPRemoteCommandHandlerStatus(MPRemoteCommandEvent * _Nonnull event) { NSLog(@"PREVIoUSSS"); return MPRemoteCommandHandlerStatusSuccess; }];
解决方法
看起来你有多个你调用代码的对象实例,例如.如果您按轨道推送新的UIViewController.旧视图控制器可能仍然存在并再次调用处理程序.
尝试将代码放入
- (void)viewDidAppear:(BOOL)animated
然后像这样禁用它
- (void)viewWillDisappear:(BOOL)animated { MPRemoteCommandCenter *commandCenter = [MPRemoteCommandCenter sharedCommandCenter]; [commandCenter.nextTrackCommand removeTarget:self]; [commandCenter.prevIoUsTrackCommand removeTarget:self]; }