【问题标题】:How to remove delay from UIAlertController?如何从 UIAlertController 中删除延迟?
【发布时间】:2017-01-09 05:18:42
【问题描述】:

点击表格单元格后,我会延迟 4 到 5 秒来显示警报视图。下面是代码

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

如何避免这种延迟?

【问题讨论】:

  • 动画看起来慢吗?
  • 不,是需要显示的动作方法,即'Ok'和alert controller中的'Cancel'。
  • 你在任何地方的后台线程上吗?

标签: ios swift uitableview uialertcontroller


【解决方案1】:

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

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

【讨论】:

  • 这应该不是必需的,因为表视图委托方法应该只在主队列上调用。 @leaner122 您是否自己显式调用了 didSelectRowAt 委托方法?
  • @rmaddy 你是对的,这不应该是必要的。但是我遇到了同样的行为,当它创建 UIAlertViewController 并呈现它时,我可以看到它已经在主线程上,但我遇到了同样的问题,该问题已通过调度解决。可能是一个错误,并且是一个旧错误:stackoverflow.com/questions/26449724/…
  • 你是绝对正确的,这确实解决了某些原因。
【解决方案2】:

在主队列中编写代码以呈现 UIAlertViewController。

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

【讨论】:

  • 你能详细说明吗?
猜你喜欢
  • 2014-12-14
  • 2021-07-26
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-09-04
  • 2021-09-29
相关资源
最近更新 更多