gpt4 book ai didi

swift 3 : Need to pass data to next view controller without moving to it

转载 作者:行者123 更新时间:2023-11-30 12:16:07 24 4
gpt4 key购买 nike

我需要传递来自一个 View Controller 的数据、来自 Alamofire 请求的数据,并且必须发送到下一个 View Controller 中的 UIlabels。我还不想转换到该 View Controller 。但目标 View Controller 必须能够读取和使用 Alamofire 请求中的数据。我用 segue 尝试了它,但我没有让它工作,我怀疑这是否是正确的方法。

这就是我所拥有的:

Alamofire.request(URL_SCAN_ID, method: .post, parameters: ScanParameters, encoding: JSONEncoding.default) .responseJSON
{

response in

//printing response
print(response.request!)
print(response.response!)
print(response.data!)
print(response.result)
print(response.error)

//getting the json value from the server
let value = response.result.value
print(value!)
let json = JSON(value!)

let productdesc0:JSON = json["productdesc"]
let productdescString = productdesc0.string



self.performSegue(withIdentifier: "ScanInfo", sender: productdescString)


func prepareForSegue(segue: UIStoryboardSegue, sender: Any){
if (segue.identifier == "ScanInfo"){
let destinationVC = segue.destination as! ScanInfoViewController
destinationVC.productdescription01 = productdescString!
print("oeiii",sender)
}


}

我认为数据甚至没有发送到segue...打印没有显示任何内容。

最佳答案

在您的代码中,准备函数位于闭包内。它必须在文件级别。如果给定的代码来自 View Controller ,则它必须类似于 -

override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
if (segue.identifier == "ScanInfo"){
let destinationVC = segue.destination as! ScanInfoViewController
destinationVC.productdescription01 = productdescString!
print("oeiii",sender)
}
}

关于 swift 3 : Need to pass data to next view controller without moving to it,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45423813/

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