Ashok Ashok - 3 months ago 26
iOS Question

How to pass data from viewcontroller A to another viewcontroller B using swift 3

I am creating instance of ViewControllerB from ViewControllerA using

instantiateViewControllerWithIdentifier(identifier: String)
function.

let storyboard = UIStoryboard(name: "Main", bundle: nil)
let vc = storyboard.instantiateViewControllerWithIdentifier("vcB") as VCB;
rootController!.presentViewController(vc, animated: true, completion: nil)


class VCB: UIViewController {

required init?(coder aDecoder: NSCoder){
super.init(coder: aDecoder)
}

}


I want to access value which i have passed in my ViewControllerB how can i achieve this.

i alredy gone through
Passing Data between View Controllers link but the answers in objective c.

Answer

You just can declare a var in your VCB viewController and inject data to this property

let storyboard = UIStoryboard(name: "Main", bundle: nil)
let vc = storyboard.instantiateViewControllerWithIdentifier("vcB") as VCB;

vc.yourData = SOME_DATA

rootController!.presentViewController(vc, animated: true, completion: nil)


class VCB: UIViewController {

var yourData: AnyObject?

required init?(coder aDecoder: NSCoder){
    super.init(coder: aDecoder)
  }

}
Comments