How to remove delay from UIAlertController?

自闭症网瘾萝莉.ら 提交于 2019-12-10 23:47:20

问题


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)
}

How to avoid this delay?


回答1:


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)
}



回答2:


Write code in main queue to present UIAlertViewController.

DispatchQueue.main.async {
   //Write your code here.
}


来源:https://stackoverflow.com/questions/41541444/how-to-remove-delay-from-uialertcontroller

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!