MPRemoteCommandCenter在iOS中多次调用处理程序

前端之家收集整理的这篇文章主要介绍了MPRemoteCommandCenter在iOS中多次调用处理程序前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
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];
}
原文链接:https://www.f2er.com/iOS/334875.html

猜你在找的iOS相关文章