Create a new singleView Xcode project.
Open Main.storyboard and drag and add a second view controller.
For the second view controller on Main.storyboard create a new COCOA TOUCH CLASSS (set to be a subclass of: UIViewController and Language: Swift) file with a name SecondViewController.
Make sure it is a subclass of UIViewController and set its Identity->Storyboard ID to SecondViewController.
We want to pass a String value from one View Controller to another when user taps on a button. Let’s create a button first.
Open Main.storyboard and select first ViewController and from the Objects library drag and drop UIButton and UITextField on the View
Open Assistant Editor and "control drag" from the button to the ViewController in Assistant editor to create an Action for the Button (named "firstButton").
While Assistant Editor is open, "control drag" to create an outlet for UITextField with a name myInfoText
Embed first ViewController into Navigation Controller, by selecting the first ViewController on the Main.storyboard and then from the top menu select Editor -> Embed in -> Navigation Controller.
The Navigation Controller will let us have the Back button which we can use to go back from SecondViewController to first ViewController.
Now when user taps on our button a function with a name firstButton will be called.
We want to pass to our SecondViewController a String value, so this is what we will do.
Inside of Second View Controller:
Create a property of type String with a name myStringValue
To keep this example simple we will simply print the value of myStringValue property inside the viewWillAppear function. The viewWillAppear function will be called automatically every time when SecondViewController is about to appear on the screen.
Here is how my SecondViewController now looks:
Inside of firstButton function which is in first ViewController:
We will write code to check if myTextField is not empty
Create an instance of SecondViewController and set the value of its variable myStringValue to the value from myTextField
And finally we will Present(push) SecondViewController to user.
Here is how my first ViewController Add the following IBAction & code after the didReceiveMemoryWarning() function:
Finish the first ViewController with the new code between the curly braces after the return you alrady put into the button action:
Now run your application, type something into UITextField and tap on a button to see how it works. If the text field is not empty, user will be taken to a SecondViewController and the value set to myStringValue will be printed out to Debug area.