如何从 UIAlertController 中删除延迟? [英] How to remove delay from UIAlertController?

查看:34
本文介绍了如何从 UIAlertController 中删除延迟?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

点击表格单元格后,我的警报视图延迟了 4 到 5 秒.下面是代码

After I tap table cells, I am getting a delay of 4 to 5 seconds for the alert view to display. Below is the code

func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath){

let cell = tableView.cellForRow(at: indexPath)!
let alertController = UIAlertController(title: nil, message: nil, preferredStyle: .alert)

let ok = UIAlertAction(title: "Ok", style: .default, handler: { (action) -> Void in
    let cell = tableView.cellForRow(at: indexPath)!
    })
let cancel = UIAlertAction(title: "Cancel", style: .cancel, handler: { (action) -> Void in
})
   alertController.addAction(ok)
   alertController.addAction(cancel)

present(alertController, animated: true, completion: nil)
}

如何避免这种延迟?

推荐答案

当我们处理 UI 时,重要的是它必须在主要步骤上完成.因此,只需复制显示警报的代码并粘贴到调度主线程块中即可.

When we deal with UI, then its important that it must be done on main tread. So just copy your code of showing alert and paste in dispatch main thread block.

DispatchQueue.main.async {
   let alertController = UIAlertController(title: nil, message: nil, preferredStyle: .alert)

let ok = UIAlertAction(title: "Ok", style: .default, handler: { (action) -> Void in
    let cell = tableView.cellForRow(at: indexPath)!
    })
let cancel = UIAlertAction(title: "Cancel", style: .cancel, handler: { (action) -> Void in
})
   alertController.addAction(ok)
   alertController.addAction(cancel)

present(alertController, animated: true, completion: nil)
}

这篇关于如何从 UIAlertController 中删除延迟?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆