iOS presentViewController:animated:completion:延迟问题

来源:互联网 发布:码云的独立域名 编辑:程序博客网 时间:2024/06/02 09:00

iOS presentViewController:animated:completion:延迟问题

在 iOS 中,当使用

- (void)presentViewController:(UIViewController *)viewControllerToPresent animated: (BOOL)flag completion:(void (^ __nullable)(void))completion

方法进行界面跳转的时候,有时候会出现延迟,这个延迟有时候会有好几秒的时间才会执行 completion,有时候干脆就一直不会跳转。
例如:在tableview的点击方法中执行

DLAlertViewController *alertViewController = [[DLAlertViewController alloc] initWithNibName:nil bundle:nil];        ......        [self presentViewController:alertViewController animated:YES completion:^{        }];

alertViewController 跳转延迟很长时间,有时候干脆就不跳转了。但让人头疼的是点击cell的时候,在alertViewController跳转延迟过程中,滑动一下tableview或者再次点击一下cell,alertViewController会立即跳转。
查找了很长时间,总算得出一个原因:由于某种原因,presentViewController跳转时completion的内容并不会真的马上触发执行,除非有一个主线程事件触发这种消费。比如在弹出慢的时候,你随便点击一下屏幕,马上就能弹出来 。
所以得出相应的解决方法:

  • 1.在主线程中执行跳转:
    WS(weakSelf);    dispatch_async(dispatch_get_main_queue(), ^{        DLAlertViewController *alertViewController = [[DLAlertViewController alloc] initWithNibName:nil bundle:nil];        ......        [weakSelf presentViewController:alertViewController animated:YES completion:^{        }];    });
  • 2.在执行跳转前唤醒主线程。

    /** WakeUpTheMainThread 方法什么都不执行,它的作用只是唤醒主线程 */
    [self performSelectorOnMainThread:@selector(WakeUpTheMainThread) withObject:nil waitUntilDone:NO];
0 0
原创粉丝点击