I am trying to transfer data from the textfield of one View Controller to the label from another.
How can I call the View Controller instance from the code of the o
You need to create a Segue between View Controllers:
ViewController A.Control, click ViewController A, drag and drop the blue line to ViewController B. If ViewController A is embedded in a NavigationController, select "show" from the menu that appears when you let go. Otherwise, select "present modally."Now, when you want to trigger the segue on ViewController A, you just need to call (maybe on the tap of a button):
@IBAction func buttonTapped() {
self.performSegueWithIdentifier("DetailSegue", sender: self)
}
To pass a value to ViewController B, override the prepareForSegue:sender method on ViewController A:
override func prepareForSegue(segue: UIStoryboardSegue!, sender: AnyObject!) {
if segue.identifier == "DetailSegue" {
var viewControllerB = segue.destinationViewController as ViewControllerB
viewControllerB.text = self.textField.text
}
}
Pretty straightforward.
Note that for this to work, your ViewController B class should look something like this:
class ViewControllerB: UIViewController {
ver label = UILabel(...)
var text: String?
override func viewDidLoad() {
super.viewDidLoad()
label.text = text!
}
}
Hope this helps.