如何传递json数据的其他视图控制器

时间:2016-11-10 11:46:49

标签: ios json swift swift2

大家都从服务器获取数据,即:

{


  error = 0;

  "error_msg" = "Successfully Login";

 success = 1;

  user =     {

  DriverID = 35;

 DriverName = "Home  nath";

"com_id" = 2;

};

}

现在我需要将DriverName的数据传递给标签box.how中的其他视图控制器来执行此操作吗?

1 个答案:

答案 0 :(得分:2)

在目标视图控制器中创建一个string类型的变量。

<强> DestinationViewController.Swift

class DestinationViewController: UIViewController {

    var driverName: String?

    override func viewDidLoad() {
        super.viewDidLoad()

        print(driverName)

    }
}

<强> CurrentViewController.swift

在CurrentViewController中覆盖prepareForSegue:sender:方法。

class CurrentViewController: UIViewController {

    override func viewDidLoad() {
        super.viewDidLoad()

    }

    override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
        let destnationVC = segue.destination as! DestinationViewController
        destnationVC.driverName = "Home nath"
    }

}

学习如何将数据从CurrentViewController传递到DestinationViewController和DestinationViewController到OrignViewController,它会帮助你。

参考Passing Data between View Controllers