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

时间:2016-08-29 21:55:47

标签: ios json swift

我需要将此数据(id和status)发送到下一个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和parse的方法,然后使用方法发送到下一个视图控制器。



     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控制器的方法



        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)

        }

谢谢你们

1 个答案:

答案 0 :(得分:0)

将数据作为NSDictionary发送,在下一个视图控制器中创建一个字典栏并在segue属性中设置此。