我使用 Xcode 10 和 Swift 4.2
在 UIViewController 的 viewDidload() 中,我声明了一个观察者:
override func viewDidLoad() {
super.viewDidLoad()
NotificationCenter.default.addObserver(self, selector: #selector(OtherFunctionsViewController.dataImportExportFunc), name: .ImportExportdata, object: nil)
}
我从网络收到一条消息(基于 .ImportExportdata 名称),该消息将名为 ProgressLevel 的浮点值从 0.0 增加到 1.0
我已声明此值供 IOS 遵循,并且它有效。它正确打印将要更新的值
@IBOutlet weak var OtherFuncProgressView: UIProgressView!
var ProgressLevel : Float = 0.0 {
willSet // "name" is ABOUT TO CHANGE
{
print("ProgressLevel WILL be set...")
print("from current value: \(ProgressLevel)")
print("to new value: \(newValue).\n")
DispatchQueue.global(qos: DispatchQoS.QoSClass.userInteractive).async
{
DispatchQueue.main.async
{
print("dispatch queue main")
self.OtherFuncProgressView.setProgress(Float(self.ProgressLevel), animated: true)
self.OtherFuncProgressView.setNeedsDisplay()
}
}
}
}
ProgressLevel 已正确打印,但 ProgressView 从未在屏幕上更新。
出了什么问题?
感谢您的回答
我根据您的建议更改我的代码如下:
var ProgressLevel : Float = 0.0
{
willSet // "name" is ABOUT TO CHANGE
{
print("ProgressLevel WILL be set...")
print("from current value: \(ProgressLevel)")
print("to new value: \(newValue).\n")
// DispatchQueue.global(qos: DispatchQoS.QoSClass.userInteractive).async
// {
DispatchQueue.main.async
{
print("dispatch queue main value \(newValue)")
// self.OtherFuncProgressView.setProgress(Float(self.ProgressLevel), animated: true)
self.OtherFuncProgressView.setProgress(Float(newValue), animated: true)
self.OtherFuncProgressView.setNeedsDisplay()
}
// }
}
}
我得到: ProcessImportFile:文件:///Users/guydesbief/Library/Developer/CoreSimulator/Devices/17DA59AA-430F-4395-B2F3-FF2A81EEB429/data/Containers/Data/Application/91B3BA10-45C1-4090-A401-5CDA03EED8C8/tmp/2019 -2-21_2018-2-19_Mois.csv 2019-02-21 11:18:22.755903+0100 SolarPanelSurveyIos[85928:4363118] razImport 完成 进度级别将被设置... 从当前值:0.0 到新值:0.0。
2019-02-21 11:18:22.758389+0100 SolarPanelSurveyIos[85928:4363118] 统计WindowController文件流正常 2019-02-21 11:18:22.760550+0100 SolarPanelSurveyIos[85928:4363118] 无缓冲区! 调度队列主值0.0 2019-02-21 11:18:22.760800+0100 SolarPanelSurveyIos[85928:4363118] Statistic WindowController 没有从文件中读取更多数据! 2019-02-21 11:18:22.761242+0100 SolarPanelSurveyIos[85928:4363118] 字段标题:导入记录以结尾 char Nbfields = 4 ,当前记录:0 2019-02-21 11:18:22.761342+0100 SolarPanelSurveyIos[85928:4363118] 导入结束 40 条记录,38 条最大记录
进度级别将被设置... 从当前值:0.0 到新值:0.05263158。
进度级别将被设置... 从当前值:0.05263158 到新值:0.13157895。
进度级别将被设置... 从当前值:0.13157895 到新值:0.21052632。
进度级别将被设置... 从当前值:0.21052632 到新值:0.28947368。
进度级别将被设置... 从当前值:0.28947368 到新值:0.36842105。
进度级别将被设置... 从当前值:0.36842105 到新值:0.4473684。
进度级别将被设置... 从当前值:0.4473684 到新值:0.5263158。
进度级别将被设置... 从当前值:0.5263158 到新值:0.6052632。
进度级别将被设置... 从当前值:0.6052632 到新值:0.68421054。
进度级别将被设置... 从当前值:0.68421054 到新值:0.7631579。
进度级别将被设置... 从当前值:0.7631579 到新值:0.84210527。
进度级别将被设置... 从当前值:0.84210527 到新值:0.92105263。
进度级别将被设置... 从当前值:0.92105263 到新值:1.0。
2019-02-21 11:18:23.631275+0100 SolarPanelSurveyIos[85928:4363118] 执行时间:871867
dataImportExportFunc 没有更多数据记录数:0/39.000000 execTime:0.8719
调度队列主值0.05263158
调度队列主值0.13157895
调度队列主值0.21052632
调度队列主值0.28947368
调度队列主值0.36842105
调度队列主值0.4473684
调度队列主值0.5263158
调度队列主值0.6052632
调度队列主值0.68421054
调度队列主值0.7631579
调度队列主值0.84210527
调度队列主值0.92105263
调度队列主值1.0
调度队列仅在进程结束时清空,我想实时清空它 有任何建议欢迎留言
最佳答案
willSet
在值更新之前被调用,因此最好在闭包内使用 newValue
:
var progressLevel: Float = 0.0 {
// "name" is ABOUT TO CHANGE
willSet {
print("ProgressLevel WILL be set...")
print("from current value: \(progressLevel)")
print("to new value: \(newValue).\n")
DispatchQueue.main.async {
print("dispatch queue main")
self.OtherFuncProgressView.setProgress(Float(newValue, animated: true)
self.OtherFuncProgressView.setNeedsDisplay()
}
}
}
关于swift - 从网络更新 UIProgressView,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54777244/