无法使用 didSet swift 在主线程中更新 UI
Cannot update UI in main thread with didSet swift
我正在尝试在主线程中更新 UILabel 和 UIImageView,但它不起作用。
我有两个 ViewController(我在这里称它们为 ChooseCountryVC 和 DisplayCountryVC),在 ChooseCountryVC 中,用户可以选择他们喜欢的国家,在 DisplayCountryVC 中,我想在标签中显示国家名称,在 imageView 中显示国家国旗。
基本流程如下。
- 在 DisplayCountryVC 中点击 "Choose a country" 按钮
- 现在的 ChooseCountryVC 基本上是所有国家的 tableView,用户可以选择一个国家
- 当用户选择一个国家时,在 DisplayCountryVC 中设置 属性 调用 "countryCode" 并关闭 ChooseCountryVC 以返回到 DisplayCountryVC
- 在 DisplayCountryVC "countryCode" 的 didSet 函数中,将 UI 更新过程添加到主线程。
与此事相关的代码示例如下。
●选择国家VC
// when user has chosen a country and dismiss this VC
let displayCountryVC = DisplayCountryVC()
displayCountryVC.countryCode = self.chosenCountryCode
self.dismiss(animated: true, completion: nil) // go back to DisplayCountryVC
●DisplayCountryVC
var countryCode: String? {
didSet {
guard let countryCode = countryCode else {return}
let countryName = self.getCountryName(with: countryCode) // this function provides countryName with no problem
let flagImage = self.getFlag(with: countryCode) // this function provides flag image with no problem
DispatchQueue.main.async {
self.imageView.image = flagImage
self.label.text = countryName
print("label's text is", self.label.text) // somehow, this prints out country's name correctly in console but apparently UI has not been updated at all
}
}
}
如果有人知道我的代码有什么问题或需要更多信息,请告诉我。
// when user has chosen a country and dismiss this VC
let displayCountryVC = DisplayCountryVC()
displayCountryVC.countryCode = self.chosenCountryCode
self.dismiss(animated: true, completion: nil) // go back to DisplayCountryVC
在这里你初始化一个新的视图控制器并为其设置值。
您选择的国家代码设置为新的视图控制器,而不是您原来的 DisplayCountryVC。
在您的 ChooseCountryVC 关闭后,您返回到原来的 DisplayCountryVC。这个新的 DisplayCountryVC 没有出现或使用。
您需要编写委托或回调以将所选国家/地区代码传递给您的原始 DisplayCountryVC。
我正在尝试在主线程中更新 UILabel 和 UIImageView,但它不起作用。
我有两个 ViewController(我在这里称它们为 ChooseCountryVC 和 DisplayCountryVC),在 ChooseCountryVC 中,用户可以选择他们喜欢的国家,在 DisplayCountryVC 中,我想在标签中显示国家名称,在 imageView 中显示国家国旗。
基本流程如下。
- 在 DisplayCountryVC 中点击 "Choose a country" 按钮
- 现在的 ChooseCountryVC 基本上是所有国家的 tableView,用户可以选择一个国家
- 当用户选择一个国家时,在 DisplayCountryVC 中设置 属性 调用 "countryCode" 并关闭 ChooseCountryVC 以返回到 DisplayCountryVC
- 在 DisplayCountryVC "countryCode" 的 didSet 函数中,将 UI 更新过程添加到主线程。
与此事相关的代码示例如下。
●选择国家VC
// when user has chosen a country and dismiss this VC
let displayCountryVC = DisplayCountryVC()
displayCountryVC.countryCode = self.chosenCountryCode
self.dismiss(animated: true, completion: nil) // go back to DisplayCountryVC
●DisplayCountryVC
var countryCode: String? {
didSet {
guard let countryCode = countryCode else {return}
let countryName = self.getCountryName(with: countryCode) // this function provides countryName with no problem
let flagImage = self.getFlag(with: countryCode) // this function provides flag image with no problem
DispatchQueue.main.async {
self.imageView.image = flagImage
self.label.text = countryName
print("label's text is", self.label.text) // somehow, this prints out country's name correctly in console but apparently UI has not been updated at all
}
}
}
如果有人知道我的代码有什么问题或需要更多信息,请告诉我。
// when user has chosen a country and dismiss this VC
let displayCountryVC = DisplayCountryVC()
displayCountryVC.countryCode = self.chosenCountryCode
self.dismiss(animated: true, completion: nil) // go back to DisplayCountryVC
在这里你初始化一个新的视图控制器并为其设置值。
您选择的国家代码设置为新的视图控制器,而不是您原来的 DisplayCountryVC。
在您的 ChooseCountryVC 关闭后,您返回到原来的 DisplayCountryVC。这个新的 DisplayCountryVC 没有出现或使用。
您需要编写委托或回调以将所选国家/地区代码传递给您的原始 DisplayCountryVC。