将String / Object值传递给另一个ViewController

时间:2016-12-26 17:59:57

标签: ios swift uiviewcontroller swift3 uistoryboard

我正在使用它打开另一个ViewController:

let mainStoryboard: UIStoryboard = UIStoryboard(name:"Main", bundle:nil)
let homeViewController: UIViewController = mainStoryboard.instantiateViewController(withIdentifier: "IssueViewController")
self.present(homeViewController, animated: true, completion: nil)

除此之外,我还需要将Person对象和String值传递给第二个ViewController。

struct Person {
  var Name: String
  var Details: String
}

将Person对象附加到现有代码需要做哪些更改?

编辑:这是第二个ViewController

我正在尝试从此视图中检索值

class IssueViewController: UIViewController {

    var person: Person = Person();

    override func viewDidLoad() {
        super.viewDidLoad()

    }

}

1 个答案:

答案 0 :(得分:1)

//在第一个控制器中更改

let mainStoryboard: UIStoryboard = UIStoryboard(name:"Main",bundle:Bundle.main)
let homeViewController: IssueViewController = mainStoryboard.instantiateViewController(withIdentifier: "IssueViewController") as! IssueViewController
homeViewController.person = Person(Name:"ABC",Details:"XYZ")
homeViewController.bindWithData(yourStringObject)
self.present(homeViewController, animated: true, completion: nil)

//在第二个视图控制器中更改

class IssueViewController: UIViewController {

var person: Person = Person(Name:"",Details:"");

override func viewDidLoad() {
    super.viewDidLoad()
    print(person.Name)
    print(person.Details)
}
func bindWithData(yourStringObject:String){
//your code here.
}
}