Jay
Jay

Reputation: 5084

Passing a String/Object value to another ViewController

I am opening another ViewController using this:

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

Along with this, I need to pass a Person object and a String value to the 2nd ViewController.

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

What changes do I need to do to attach a Person object to my existing code?

EDIT: This is the 2nd ViewController

I am trying to retrieve the values from this view

class IssueViewController: UIViewController {

    var person: Person = Person();

    override func viewDidLoad() {
        super.viewDidLoad()

    }

}

Upvotes: 1

Views: 73

Answers (1)

Aman Gupta
Aman Gupta

Reputation: 985

//changes in first controller

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)

//changes in second view controller

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.
}
}

Upvotes: 1

Related Questions