gpt4 book ai didi

Swift,标签在回调函数中不刷新

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

我想在循环运行时更改/重新加载事件指示器上的标签。但我认为它不会重新加载,因为“self.netUtil.callWebservice(urlStr) {(data)”。

我的代码如下——————————————

     if netUtil.isConnectedToNetwork() == true
{
self.showActivityIndicator(self.view,message: "Synch is progress...")

//Logic toc all Web Service
var urlStr: String = "\(constants.SERVER_URL)"
self.container.addSubview(self.lblProcess)
self.netUtil.callWebservice(urlStr) {(data) -> Void in

self.fileMgr.createFileDirectory("/Surveyor")
self.filePath = self.fileMgr.createFile(self.fileMgr.getSurveyourPropListXmlName(""))

self.fileMgr.writeFile(data, filenamePath: self.fileMgr.getSurveyourPropListXmlName(""))
self.xmlNSData = data.dataUsingEncoding(NSUTF8StringEncoding)!

self.beginParsing()
self.loadPropertyMap()
var unitCount = 0


for Prop: Property in self.properties
{

unitCount++

for view in self.view.subviews
{
if view is UILabel
{
view.removeFromSuperview()
}
}

/* refresh label code start here */
dispatch_async(dispatch_get_main_queue(), {
//Run UI Updates
self.unitCount++
println("unitCount \( self.unitCount)")
var totCount = self.properties.count
var message = "Synchronization \(self.unitCount)/\(totCount) buildings completed"
self.lblProcess.text = message
self.lblProcess.setNeedsDisplay()

})

/* refresh label code End here */

//Backup code to be shfted while pushing in web service
self.fileMgr.getSurveyorUnitFiles("", proCode: Prop.proCode as String, isCreateBackup: true)

//back up code ended
self.callUnitsWebservice(Prop.proCode as String) //another web service cal

}
self.lblProcess.removeFromSuperview()

self.hideActivityIndicator()
}

附加 println 的输出

属性数量 > 10单位计数 1单位计数 2单位数 3单位数 4单位数 5单位数 6单位数 7单位数 8单位数 9单位数 10

如果我将标签放在循环之外,那么它会重新加载...... 我应该如何在循环中重新加载标签,以便它在每个循环兴奋时发生变化?

感谢您提前提供帮助

最佳答案

您只能从主线程刷新 UI。

self.netUtil.callWebservice(urlStr) {(data) -> Void in} 作为 block 运行,不在主线程中。

你必须在 block 内做这样的事情:

        //Logic synchronization all Web Service
var urlStr: String = "\(constants.SERVER_URL)"

//Add the view. Don't forget to remove it when the looping is done.
self.container.addSubview(self.lblProcess)

self.netUtil.callWebservice(urlStr) {(data) -> Void in //I suspect this method to reason why my label is not refreshing

self.beginParsing()
var unitCount = 0

for Prop: Property in self.properties
{

unitCount++

for view in self.view.subviews
{
if view is UILabel
{
view.removeFromSuperview()
}
}

/* refresh label code start here */
dispatch_async(dispatch_get_main_queue(), {
//Run UI Updates
var totCount = self.properties.count
var message = "Synchronization \(unitCount)/\(totCount) buildings completed"
self.lblProcess.text = message
self.lblProcess.setNeedsDisplay()
})
/* refresh label code End here */

//Backup code to be shfted while pushing in web service
self.fileMgr.getSurveyorUnitFiles("", proCode: Prop.proCode as String, isCreateBackup: true)

//back up code ended
self.callUnitsWebservice(Prop.proCode as String) //another web service cal

}

self.hideActivityIndicator()
}

关于Swift,标签在回调函数中不刷新,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31179101/

26 4 0