Here's how you do it in 12 easy steps:
1) Yes. I recommend using a Navigation Controller
. Start a new project with a Single View Application.
2) Select the ViewController
in the Storyboard and from the Menu bar above select Editor->Embed In->Navigation Controller
.
3) Drag out a UIButton
and a UITextField
and put them in your first ViewController
.
4) You'll want an IBOutlet
to your text field so that you can read the text from it. Show the Assistant editor by clicking on the Tuxedo icon in the upper right corner of Xcode. Click on the ViewController
in the Storyboard. The right editor pane will show the code for ViewController
. Hold down the Control key and drag from the text field to the code in the right pane. Let go on the mouse button when you are in the space just below class ViewController : UIViewController {
. In the popup, set Connection to Outlet, set the name to textField
and press Connect. This will add the line @IBOutlet weak var textField: UITextField!
to your ViewController
.
5) Now it is time to add a second view controller. From the menu select File->New->File...
. In the dialog, make sure Source under iOS is selected on the left, and choose Cocoa Touch Class and press Next. Name the class SecondViewController
and make it a subclass of UIViewController
. Press Next and then Create.
6) In the Storyboard, drag out a UIViewController
and drop it to the right of the first ViewController
. Select the new ViewController
and open the Identity Inspector by clicking on the 3rd icon from the left in the row of icons below the Tuxedo icon. (Hint: if you hover of the icons it will tell you what they do). Change the class to SecondViewController
.
7) In the Storyboard, control-drag (hold down control and drag) from the button in the first ViewController
to the new SecondViewController
. In the pop-up select Show as the Action Segue.
8) Drag a Label out and put it into the SecondViewController
. Add an IBOutlet
to it like you did in step 4 and call it mylabel.
9) In the code for SecondViewController
add a new instance variable like so: var firstVCtext = ""
10) In the first ViewController
add this method:
override func prepareForSegue(segue: UIStoryboardSegue!, sender: AnyObject!) {
let secondVC = segue.destinationViewController as SecondViewController
secondVC.firstVCtext = textField.text
}
11) In SecondViewController
, add this line of code to the viewDidLoad
method: mylabel.text = firstVCtext
12) Run the program!