如何将数据从一个视图控制器传递到另一个?

时间:2017-01-13 04:45:52

标签: swift segue

如何将数据从一个视图控制器的数组传递到另一个视图控制器?

FirstViewController

var myArray = ["some data"]

override func prepare(for segue: UIStoryboardSegue, sender: Any?){
  let destinationVC = segue.destination as! SecondViewController
  destinationVC.passedArray = myArray
}

SecondViewController

var passedArray = [String]()

override func viewDidLoad(){
  super.viewDidLoad()
  print(passedArray)
}

1 个答案:

答案 0 :(得分:0)

您可以尝试使用此解决方案进行数据传输。

FirstViewController.swift

let str = "Hello It's Done"
func callsecondViewController()
    {
        let controller = storyboard?.instantiateViewController(withIdentifier: "second") as! secondViewController
        self.present(controller, animated: true, completion: nil)
    }
func temp() -> NSString 
    {
        return str as NSString
    }
SecondViewController.swift

var data = [NSString]()
    override func viewDidLoad() 
    {
        super.viewDidLoad()
        let view = ViewController()
        data = [view.temp()]
        print(data)
    }

你得到输出字符串为Hello It's Done