gpt4 book ai didi

ios - 无法在调度队列中手动控制 performSegue()

转载 作者:行者123 更新时间:2023-11-28 07:23:11 24 4
gpt4 key购买 nike

我有一个按钮对象,它运行一个用于计算的后台线程和一个用于 UI 更新的主线程。全部完成后,我想使用 performSegue(withIdentifier: "thirdViewController", sender: self) 移动到另一个 View Controller ,但它在 dispatchQueue 中不起作用.我以前多次使用 performSegue() 在 View Controller 之间切换,我总是可以手动控制它。相反,它几乎立即移动到下一个 ViewController,而没有先完成计算。

我尝试使用 self.present(thirdViewController(), animated: true, completion: nil)self.navigationController?.pushViewController(thirdViewController(), animated: true) 来更改 ViewController,但他们只给了我一个黑屏。

@IBAction func generateText(_ sender: Any) {
DispatchQueue.global(qos: .userInteractive).async {
//computation code
DispatchQueue.main.sync {
//update UI code
self.performSegue(withIdentifier: "Segue2", sender: self)
}
}
}

最佳答案

这种行为可能是由几个不同的问题引起的

  1. 您可能同时将 @IBAction 和 segue 附加到此按钮。确保您只有 @IBAction。 segue 本身不应附加到按钮,而应源自场景的“ View Controller ”导出(在顶部栏中)而不是来自单元格或按钮。如果是这种情况,请删除现有的 segue,并从场景上方的栏中添加另一个:

    enter image description here

    然后为该 segue 提供一个标识符,然后您可以通过编程方式调用该标识符。

  2. 或者,“计算代码”以某种方式允许它在完成之前流向 DispatchQueue.main.async 调用(即计算代码正在启动本身是异步的东西) .

没有看到你的代码,我们不能说。但是,从您随后的评论来看,第一个观察结果似乎是您案例中问题的根源,但为了 future 的读者,我已经概述了这两种选择。

关于ios - 无法在调度队列中手动控制 performSegue(),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57400521/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com