美文网首页iOS学习iOS Developer
iOS UIAlertController 弹出延迟

iOS UIAlertController 弹出延迟

作者: 圆脸黑猫警长 | 来源:发表于2017-01-19 15:45 被阅读2011次

在点击tableview上的一个cell后弹出alert,会发现有延迟的问题,或者点击没有反应,随便再点击一下才会弹出。而且只有在cell.selectionStyle = UITableViewCellSelectionStyleNone;时才会出现。查资料得出以下推断:

点击事件发生后没有处理UI变动,或者加到其他线程中了,主线程经过一次(或多次)循环后才发现此需要刷新UI,然后才会刷新UI。

解决方案:

方案一
不要设置为UITableViewCellSelectionStyleNone,在点击事件中用` [tableView deselectRowAtIndexPath:indexPath animated:YES];去除选中效果即可。

方案二
将弹出的代码直接放到主线程执行
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
UIAlertController *alertC = [UIAlertController alertControllerWithTitle:@"是否退出" message:@"" preferredStyle:UIAlertControllerStyleAlert];

    UIAlertAction *actionCancel = [UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:nil];
    [alertC addAction:actionCancel];
        
    UIAlertAction *actionConfirm = [UIAlertAction actionWithTitle:@"退出" style:UIAlertActionStyleDestructive handler:^(UIAlertAction * _Nonnull action)
                                        {
                                            // do sth
                                        }];
    [alertC addAction:actionConfirm];

    dispatch_async(dispatch_get_main_queue(), ^{        
    [self presentViewController:AlertView animated:YES completion:nil];
    });
}
有大神知道具体原因或者有不同思路欢迎留言探讨学习。

参考资料:http://stackoverflow.com/questions/26449724/uialertcontroller-showing-with-delay

相关文章

网友评论

    本文标题:iOS UIAlertController 弹出延迟

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