gpt4 book ai didi

ios - Swift 自定义 updateUI() 函数在 viewDidLoad 上不起作用

转载 作者:行者123 更新时间:2023-11-30 13:52:06 27 4
gpt4 key购买 nike

我正在开发一个应用程序,它应该从我的服务器请求一些数据。我使用 Alamofire 来执行此操作,然后使用 SWXMLHash 来解析 XML 数据。有两个 View Controller ,在第一个 View Controller 上我可以编写一个发货编号,然后重写函数prepareForSegue并将该编号发送到下一个 View Controller ,该 View Controller 应该显示来自服务器的数据并在viewDidLoad上更新UI,但事实并非如此。哪里有问题?

我的类(class):

class Shipment {
private var _shipmentNumber: String!
private var _shipmentStatus: String!
private var _trackURL: String!

var shipmentNumber: String {
if _shipmentNumber == nil {
_shipmentNumber = ""
}
return _shipmentNumber
}

var shipmentStatus: String {
if _shipmentStatus == nil {
_shipmentStatus = ""
}
return _shipmentStatus
}

init(spNumber: String) {
self._shipmentNumber = spNumber

_trackURL = "..."
}

func requestXmlInformation(completed: DownloadComplete) {
let url = NSURL(string: _trackURL)!
Alamofire.request(.GET, url).responseData { response in

if let xmlToParse = response.data as NSData! {
let xml = SWXMLHash.parse(xmlToParse)

do {
let xmlSpWeight = try xml["fmresultset"]["resultset"]["record"]["field"].withAttr("name", "ТotalWeight")["data"].element!.text! as String

self._shipmentStatus = xmlSpStatus
print(self._shipmentStatus)
} catch let err as NSError {
print(err.debugDescription)
}
}
}
}
}

我的第二个 View Controller

@IBOutlet weak var numberLbl: UILabel!
@IBOutlet weak var weightLbl: UILabel!
@IBOutlet weak var statusLbl: UILabel!
@IBOutlet weak var packageQtyLbl: UILabel!

var shipment: Shipment!

override func viewDidLoad() {
super.viewDidLoad()
shipment.requestXmlInformation { () -> () in
self.updateUi()
print(self.statusLbl.text)
}
}

更新UI函数:

func updateUi() {
numberLbl.text = shipment.shipmentNumber
weightLbl.text = shipment.shipmentWeight
statusLbl.text = shipment.shipmentStatus
packageQtyLbl.text = shipment.shipmentPackageQty
}

它在终端中打印数据,但我认为 updateUI 功能不起作用。

最佳答案

确保在主线程上调用 requestXmlInformation 闭包中的代码。您不应该在后台线程中更新 UI。

shipment.requestXmlInformation { () -> () in
dispatch_async(dispatch_get_main_queue(), { () -> Void in
self.updateUi()
print(self.statusLbl.text)
})
}

此外,您似乎没有在 requestXmlInformation 方法中的任何位置调用完整的闭包

关于ios - Swift 自定义 updateUI() 函数在 viewDidLoad 上不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34136951/

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