gpt4 book ai didi

ios - Button.setTitle 直到按钮被点击后才会发生

转载 作者:行者123 更新时间:2023-11-28 15:30:14 35 4
gpt4 key购买 nike

我正在尝试根据来自 API 的 JSON 响应更新按钮的标题。

JSON 请求以及按钮标题的设置发生在我的 View Controller 中的 viewDidLoad 中,但按钮的标题直到我单击按钮后才会更新。

以前有人遇到过这种情况吗?

下面发生在加载 View 中。

session.dataTask(with: request) {data, response, err in
if let data = data{
var json: Any
do{
json = try JSONSerialization.jsonObject(with: data)
//print(json)
guard let dictionary = json as? [String: Any] else{
print("oops")
return
}
guard let roster = Roster(json: dictionary) else{
print("oops2")
return
}
let member = roster.pickRandomMembers()
self.button.setTitle(member.first_name, for: .normal)

}
catch{
print(error)
}

}
}.resume()

最佳答案

试试这个。 DispatchQueue.main.async() 会强制里面的 UI 更新代码在主线程上执行

DispatchQueue.main.async() {
self.button.setTitle(member.first_name, for: .normal)
}

关于ios - Button.setTitle 直到按钮被点击后才会发生,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44788117/

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