gpt4 book ai didi

ios - 我无法将数据发送到下一个 UIViewController

转载 作者:行者123 更新时间:2023-11-30 13:07:08 28 4
gpt4 key购买 nike

我需要将此数据(id 和状态)发送到下一个 UIViewController



{"success":true,"id":37,"status":"WAITING"}

但是在输出中发生了这种情况......


id: nil // Nextview
status try: nil // nextView

ID NEXT VIEW 37 // the present view
status NEXT VIEW WAITING // the present view

id: Optional(37) // the next view
status try: Optional("WAITING") // the next view

这是我获取 json 并解析的方法,然后使用方法发送到下一个 View Controller 。



func sendOrder(jsonOrder: AnyObject){
let request = NSMutableURLRequest(URL: NSURL(string: "http://192.168.0.21:8080/easy-coffee/get-order/")!)
request.HTTPMethod = "POST"
let postString = jsonOrder
print(postString)
request.HTTPBody = postString.dataUsingEncoding(NSUTF8StringEncoding)
let task = NSURLSession.sharedSession().dataTaskWithRequest(request) { data, response, error in
guard error == nil && data != nil else {
print("error=\(error)")
return
}

if let httpStatus = response as? NSHTTPURLResponse where httpStatus.statusCode != 200 { print("statusCode should be 200, but is \(httpStatus.statusCode)")
print("response = \(response)")
}
let responseString = NSString(data: data!, encoding: NSUTF8StringEncoding)!
print(responseString)
do{
let OrderDict = try NSJSONSerialization.JSONObjectWithData(data!, options: NSJSONReadingOptions.AllowFragments) as! NSDictionary
self.orderId = OrderDict["id"] as? Int
self.orderStatus = OrderDict["status"] as? String
self.nextView(self.orderId, orderStatus: self.orderStatus)
}catch{

}

}
task.resume()
}

发送数据到下一个UIView Controller的方法



func nextView(orderId: Int, orderStatus: String){
if let waitingController = storyboard!.instantiateViewControllerWithIdentifier("waitingOrder") as? WaitingOrderViewController {
print("ID NEXT VIEW \(orderId)")
print("status NEXT VIEW \(orderStatus)")
waitingController.id = orderId
waitingController.status = orderStatus
self.presentViewController(waitingController, animated: true, completion: nil)

}

谢谢大家

最佳答案

将数据作为 NSDictionary 发送,在下一个 View Controller 中创建一个字典栏,并在 segue 属性中设置它。

关于ios - 我无法将数据发送到下一个 UIViewController,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39215795/

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