gpt4 book ai didi

ios - 在 http 响应回调后移动到另一个 ViewController

转载 作者:可可西里 更新时间:2023-11-01 00:26:55 25 4
gpt4 key购买 nike

我是一个敏捷的新手。

我想在 http 响应回调后移动到另一个新的 ViewController。

我试过..


  1. 使用 segue 或委托(delegate)协议(protocol)

    当我在 Storyboard上创建一个 segue 时,(使用 ctrl 按钮并拖动)

    移动到另一个 ViewController 比 http 响应回调更快。

    因此响应数据不会显示在新的 ViewController 上


  1. 使用 instantiateViewController() 和 present() 函数

    “非主线程上没有”错误


class RequestViewController: UIViewController {

...


@IBAction func requestButtonTouchUpInside(_ sender: UIButton) {

http.post("/v1/request", jsonBody) { (response) -> () in

print(response)

// I want to move to another ViewController with response here
}
}
}

有什么解决办法吗?

提前谢谢你。

最佳答案

@IBAction func requestButtonTouchUpInside(_ sender: UIButton) {

http.post("/v1/request", jsonBody) { (response) -> () in

DispatchQueue.main.async {
performSegue(withIdentifier: "goToOtherVC", sender: self)
}
}
}

@IBAction func requestButtonTouchUpInside(_ sender: UIButton) {

http.post("/v1/request", jsonBody) { (response) -> () in

DispatchQueue.main.async {
let vc = OtherViewController()
self.present(vc, animated: true)
}
}
}

顺便说一句,我建议在闭包中使用[weak self],以避免内存泄漏,您可以在Swift Docs 阅读更多内容。

关于ios - 在 http 响应回调后移动到另一个 ViewController,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56429739/

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