Ios 在Swift 3.0中,如何在控制器之间传递JSON数据?

Ios 在Swift 3.0中,如何在控制器之间传递JSON数据?,ios,json,swift3,uilabel,xcode8,Ios,Json,Swift3,Uilabel,Xcode8,我有这些JSON数据: {"login":"ET001","email":"email@try.com"} 在Swift 3.0中,我创建了两个文件,即LoginVC和ViewController 只有在LoginVC验证凭据后才能访问ViewController。到目前为止,我设法让登录名基于数据库中的“成功”JSON数据访问ViewController页面 但我的下一个目标是将JSON数据“[login]”从LoginVC传递到ViewController 在ViewController中

我有这些JSON数据:

{"login":"ET001","email":"email@try.com"}
在Swift 3.0中,我创建了两个文件,即LoginVC和ViewController

只有在LoginVC验证凭据后才能访问ViewController。到目前为止,我设法让登录名基于数据库中的“成功”JSON数据访问ViewController页面

但我的下一个目标是将JSON数据“[login]”从LoginVC传递到ViewController

在ViewController中,我创建了UILabel“loginLbl”以显示来自LoginVC的JSON值

如何更新我的代码

斯威夫特酒店 ViewController.swift
如何将其传递到UILabel loginLbl

一旦您确定登录数据在响应中是正确的,您需要在导航控制器中推送viewController,并在viewController中获取一个字典,并将json分配给该字典

if let json = try JSONSerialization.jsonObject(with: data!) as? [String: String] {
                        DispatchQueue.main.async {
                            let success  = Int(json["success"]!)
                            let loginvaluefromdb = json["login"]
                            if(success == 1){
                                let viewController = self.storyboard?.instantiateViewController(withIdentifier: "yourViwController") as! yourViwController
                    viewController.dictJson = json
                    self.navigationController?.pushViewController(viewController, animated: true)
                            }
您需要在prepare for segue方法中传递数据: 这是editec代码

override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
    if segue.identifier == "goToViewController" {
        if let destination = segue.destination as? ViewController {
            // Here you will copy tha data you want to pass
            destination.passedData = sender as? String
            print("Sender Value: \(sender)")
        }
    }
}

是的,我知道这个问题可能是重复的。但是我仍然对如何传递数据感到困惑@AlotJai您的代码中没有导航代码。您的意思是从LoginVC到ViewController的导航?@AlotJai是的,我正在进行导航。谢谢您的回复。让我先试试。谢谢:D“dictJson”来自何处?在您使用IBMoutlet for LoginBL的viewcontroller中,放置此dictJson属性。设dictJson=Dictionary()
if let json = try JSONSerialization.jsonObject(with: data!) as? [String: String] {
                        DispatchQueue.main.async {
                            let success  = Int(json["success"]!)
                            let loginvaluefromdb = json["login"]
                            if(success == 1){
                                let viewController = self.storyboard?.instantiateViewController(withIdentifier: "yourViwController") as! yourViwController
                    viewController.dictJson = json
                    self.navigationController?.pushViewController(viewController, animated: true)
                            }
 if(success == 1){
           self.outputLbl.text = loginvaluefromdb;
           // Here you trigger the segue
           self.performSegue(withIdentifier: "goToViewController", sender: loginvaluefromdb)
           return
         }
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
    if segue.identifier == "goToViewController" {
        if let destination = segue.destination as? ViewController {
            // Here you will copy tha data you want to pass
            destination.passedData = sender as? String
            print("Sender Value: \(sender)")
        }
    }
}