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 other View Controller? I'm working with storyboards thus I never created an instance of the View Controllers in the code? Are the instances automatically created? And what name do they have?
Thanks for your help!
Imanou Petit and Oscar Swanros already answered correctly. However there is an alternative which is rather "hacky" that I had to use to transfer data between 2 view controllers without a segue connecting them.
To obtain the root view controller of your app you can do:
From there you can get any view controller you want. For instance, if you want the second child view controller of the root view controller then you would do:
Keep in mind that this approach is rather "hacky" and probably violates the best coding practices.
You need to create a Segue between View Controllers:
ViewController A
.Control
, clickViewController A
, drag and drop the blue line toViewController B
. IfViewController A
is embedded in aNavigationController
, 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):To pass a value to
ViewController B
, override theprepareForSegue:sender
method onViewController A
:Pretty straightforward.
Note that for this to work, your
ViewController B
class should look something like this:Hope this helps.
1. If the view controller containing the textfield can call (with a segue) the view controller containing the label...
Add a new Cocoa Touch class file in your project, name it
FirstViewController
and set the following code in it:Add a new Cocoa Touch class file in your project, name it
SecondViewController
and set the following code in it:In the Storyboard, embed the first view controller in a
UINavigationController
. Link the first view controller to the second with aUIButton
or aUIBarButtonItem
. Set the name of the first view controller toFirstViewController
and the name of the second view controller toSecondViewController
. Create aUITextField
in the first view controller. Create aUILabel
in the second view controller. Link the textfield and the label to their respective declarations inFirstViewController
andSecondViewController
.2. If the view controller containing the label can call (with a segue) the view controller containing the textfield...
Here, this is a perfect protocol/delegate case. You may find a lot of stuff on StackOverflow dealing with this. However, here is a rough example.
Add a new Cocoa Touch class file in your project, name it
FirstViewController
and set the following code in it:Add a new Cocoa/Cocoa Touch class file in your project, name it
SecondViewController
and set the following code in it:In the Storyboard, embed the first view controller in a
UINavigationController
. Link the first view controller to the second with aUIButton
or aUIBarButtonItem
. Set the name of the first view controller toFirstViewController
and the name of the second view controller toSecondViewController
. Create aUILabel
in the first view controller. Create aUITextField
in the second view controller. Link the textfield and the label to their respective declarations inFirstViewController
andSecondViewController
.