我从Alamofire请求中得到了答案,我想通过UIViewController在另一个UIWebView中传递它

{
 "result":{

    "code":"0",
    "description":""
},
 "operation":[{

    "status":boolean,
    "paymentType":integer,
    "orderID":integer",
    "paymentData":"string",
    "token":"string",
    "user":[{
    "user_status":"string",
    "userID":integer,
}]
}]
}


并在paymentData中获取如下字符串:

<form action="https:..." method="post">
    <input name=".." value=".." type="hidden"/>
    <input name=".." value=".." type="hidden"/>
    <input name=".." value=".." type="hidden">
    <input name=".." value="nil" type="hidden"/>
    <input name=".." value=".." type="hidden"/>
    <input name=".." value=".." type="hidden"/>
    <input name=".." value=".." type="hidden">
    <input type=".." value="PPP" style="background: #3aaf42; border: none; color: #fff; text-align: center; width: 100px; padding: 5px; margin: -15px 0 0 -50px; display: block; top: 50%; border-radius: 3px; position: absolute; left: 50%;\"/>
</form>


ViewControllerUIWebView我要在哪里传递PaymentData我有:

var paymentDat = ""

override func viewDidLoad() {
        super.viewDidLoad()
        let htmlDat = paymentDat
 webView.loadHTMLString(htmlDat, baseURL: nil)
}


在第一个视图中,我在Alamofire中捕获了payData,如下所示:

       Alamofire.request("http://...", method: .get, parameters: params).responseJSON { response in
        if let r = response.result.value as? NSDictionary {
            let jsss = JSON(r)

            if let result = r.value(forKey: "result") as? NSDictionary {
                if let nass = result.value(forKey: "description") as? String {
  //                        print(response.result.value)

                    if let code = result.value(forKey: "code") as? String {
         //               print(code)

                        if code == "0" {
                            if let orderIDD = jsss["operation"].array![0]["orderID"].int {
    //                                    print("order--------------------- \(orderIDD)")
                                self.webb?.orderID = orderIDD
                            }
                            if let paymentData = jsss["operation"].array![0]["paymentData"].string {
   //                                    print(paymentData)
                                self.webb?.paymentDat = paymentData

                            }
                            if paymentType == 1 {
                                self.performSegue(withIdentifier: "WebView", sender: self)
                                self.alert(title: "ОК!", message: nass)

                            } else {
                                self.alert(title: "ОК!", message: nass)

                            }
                        } else {
                            self.alert(title: "Error!", message: nass)
                        }
                    }
                }
            }
        } else {
            self.alert(title: "Error connect!", message: "Error connect")
        }
    }


如何在另一个VC中显示paymentData?

最佳答案

您需要在prepare(for segue: UIStoryboardSegue, sender: Any?)方法中设置值。

在您的第一个ViewController中添加以下方法:

override func prepare(for segue: UIStoryboardSegue, sender: Any?) {

    if segue.identifier == "WebView", let webView = segue.destination as? WebViewController {
        webView.paymentDat = sender as! String
    }
}


在您的响应代码中添加以下更改:

   Alamofire.request("http://...", method: .get, parameters: params).responseJSON { response in
    if let r = response.result.value as? NSDictionary {
        let jsss = JSON(r)

        if let result = r.value(forKey: "result") as? NSDictionary {
            if let nass = result.value(forKey: "description") as? String {
  //                        print(response.result.value)

                if let code = result.value(forKey: "code") as? String {
     //               print(code)

                    if code == "0" {
                        var paymentHTML = ""
                        if let orderIDD = jsss["operation"].array![0]["orderID"].int {
//                                    print("order--------------------- \(orderIDD)")
                            self.webb?.orderID = orderIDD
                        }
                        if let paymentData = jsss["operation"].array![0]["paymentData"].string {
   //                                    print(paymentData)
                            paymentHTML = paymentData

                        }
                        if paymentType == 1 {
                            self.performSegue(withIdentifier: "WebView", sender: paymentHTML) // Pass
                            self.alert(title: "ОК!", message: nass)

                        } else {
                            self.alert(title: "ОК!", message: nass)

                        }
                    } else {
                        self.alert(title: "Error!", message: nass)
                    }
                }
            }
        }
    } else {
        self.alert(title: "Error connect!", message: "Error connect")
    }
}

09-20 12:36