0

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.

Community
  • 1
  • 1
Ashok R
  • 19,892
  • 8
  • 68
  • 68
  • 1
    There are lot of ways you can pass data from one view controller to another viewcontroller like using NSNotification center,delegate,PrepareforSegue,and using Propertylist – Sanjeet Verma Aug 11 '16 at 06:24
  • The question you linked has many Swift answers too. – rmaddy Aug 11 '16 at 06:53

3 Answers3

3

You may try

import UIKit
class ViewControllerA: UIViewController {

override func viewDidLoad() {
    super.viewDidLoad()
    // Do any additional setup after loading the view, typically from a nib.
}

@IBAction func passDataAction(sender: AnyObject) {
    let storyboard = UIStoryboard(name: "Main", bundle: nil)
    let vc = storyboard.instantiateViewControllerWithIdentifier("UIViewControllerB") as! ViewControllerB;
    vc.dataFromOtherView = "The data is passed"
    self.presentViewController(vc, animated: true, completion: nil)
}

override func didReceiveMemoryWarning() {
    super.didReceiveMemoryWarning()
    // Dispose of any resources that can be recreated.
}

}

And the other class

import UIKit
class ViewControllerB: UIViewController {

var dataFromOtherView: String = ""

override func viewDidLoad() {
    super.viewDidLoad()
    // Do any additional setup after loading the view, typically from a nib.

    print(dataFromOtherView)
}

override func didReceiveMemoryWarning() {
    super.didReceiveMemoryWarning()
    // Dispose of any resources that can be recreated.
}


}
Sofeda
  • 1,361
  • 1
  • 13
  • 23
2

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

}
Ashok R
  • 19,892
  • 8
  • 68
  • 68
iSashok
  • 2,326
  • 13
  • 21
1

Just use this code send data from one view controller to anotherview controller

   let storyboard = UIStoryboard(name: "Main", bundle: nil)
let vc=storyboard.instantiateViewControllerWithIdentifier("secondView") as! ViewControllerB;
vc.dataFromOtherView = "The data is passed"
self.presentViewController(vc, animated: true, completion: nil)