美文网首页
iOS小结(解决presentViewController延迟问

iOS小结(解决presentViewController延迟问

作者: Mr周190728 | 来源:发表于2020-07-13 13:35 被阅读0次

    在 iOS 中,当使用

    -(void)presentViewController:(UIViewController*)viewControllerToPresent animated:

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

    例如:在tableview的点击方法中执行

    - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {

    [self presentViewController:alertViewController animated:YEScompletion:^{

    }];

    }

    alertViewController 跳转延迟很长时间,有时候干脆就不跳转了。但让人头疼的是点击cell的时候,在alertViewController跳转延迟过程中,滑动一下tableview或者再次点击一下cell,alertViewController会立即跳转。

    得出一个原因:由于某种原因,presentViewController跳转时completion的内容并不会真的马上触发执行,除非有一个主线程事件触发这种消费。比如在弹出慢的时候,你随便点击一下屏幕,马上就能弹出来 。

    1.在主线程中执行跳转:

    __weak typeof(self)weakSelf = self;

    dispatch_async(dispatch_get_main_queue(), ^(void){

    [weakSelf presentViewController:alertViewController animated:YES completion:nil];

    });

    swift: 

       DispatchQueue.main.async {

             self.present(alertViewController, animated:true, completion:nil)

         }

    2.在执行跳转前唤醒主线程。

    /** WakeUpTheMainThread 方法什么都不执行,它的作用只是唤醒主线程 */

    [self performSelectorOnMainThread:@selector(WakeUpTheMainThread) withObject:nil waitUntilDone:NO];

    相关文章

      网友评论

          本文标题:iOS小结(解决presentViewController延迟问

          本文链接:https://www.haomeiwen.com/subject/dalacktx.html