如何将数据从一个视图控制器的数组传递到另一个视图控制器?
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)
}
答案 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