iOS presentViewController:animated:completion:延迟问题

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];
  • 2
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值