3

I am almost completely new to Swift and Xcode so excuse my lack of knowledge in this field. At the moment I have a view controller with a label in it. I want to add a view before that, with a text field in which a user enters a name, the app then goes to the second view with the label in which the name entered by the user is placed on the label.

I know this is very basic, but I cannot find any good solutions since I'm not quite sure what to search for. If anyone has a piece of code or a link to a good source, I'd love to see it.

Thanks!

Edit: I really know little about this. Do I need a navigation controller when using two views? Do I need two ViewController.swift files?

B_s
  • 3,026
  • 5
  • 32
  • 51

3 Answers3

3

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!

vacawama
  • 150,663
  • 30
  • 266
  • 294
  • There is a lot of detail here. If some step gives you trouble, let me know and I can elaborate or add pictures. – vacawama Aug 17 '14 at 12:57
  • I am very sorry to reply after such a long time. Thank you very much for your elaborate answer, I really appreciate you taking the time to explain it so well. I followed the steps and it works, moreover I understand how it works now better. Again: thank you! – B_s Aug 17 '14 at 15:12
1

Another good tutorial here with code and images:

http://www.jamesrambles.com/ios-swift-passing-data-between-viewcontrollers/

Steve Rosenberg
  • 19,348
  • 7
  • 46
  • 53
0

To move data from one view to another, use of a segue (pronounced seg-way) is the easiest. One example is here:

http://makeapppie.com/2014/07/01/swift-swift-using-segues-and-delegates-in-navigation-controllers-part-1-the-template/

If you Google for Swift and Segue you will likely find more examples.

Steve Rosenberg
  • 19,348
  • 7
  • 46
  • 53