Use typed text from view in another view in Swift (Xcode 6)

I am almost completely new to Swift and Xcode, so I apologize for the lack of knowledge in this area. At the moment I have a view controller with a label in it. I want to add a view before that, with a textbox where the user enters a name, the app then navigates to a second view with a label, where the name entered by the user is placed on the label.

I know this is very simple, but I can't seem to find any good solutions as I'm not really sure what to look for. If anyone has a piece of code or a link to a good source, I'd love to see it.

Thank!

Edit: I really don't know much about this. Do I need a navigation controller when using two kinds? Do I need two ViewController.swift files?

+3


source to share


3 answers


Here's how you do it in 12 easy steps:

1) Yes. I recommend using Navigation Controller

. Start a new project with the Single View app.

2) Select ViewController

in Storyboard and from the menu bar above select Editor->Embed In->Navigation Controller

.

3) Pull out a UIButton

and a UITextField

and place them in your first one ViewController

.

4) You will need IBOutlet

a text box so you can read the text. Show the helper editor by clicking the Tuxedo icon in the upper right corner of Xcode. Click on ViewController

in the Storyboard. The code for the ViewController

. Hold down the Control key and drag from the text box to the code in the right pane. Release the mouse button when you are in the space just below class ViewController : UIViewController {

. In the pop-up menu, set Connection to Outlet, set a name textField

and click Connect. This will add a line @IBOutlet weak var textField: UITextField!

to yours ViewController

.

5) Now it's time to add a second view controller. Select from the menu File->New->File...

. In the dialog box, make sure the iOS source is selected in the list and select Cocoa Touch Class and click Next. Name the class SecondViewController

and make it a subclass UIViewController

. Click Next and then Create.

6) In Storyboard, drag a UIViewController

and drop it to the right of the first one ViewController

. Select the new one ViewController

and open the Inspector Inspector by clicking on the third icon from the left in the row of icons below the Tuxedo icon. (Hint: if you hover over the icons, it will tell you what they do). Change the class to SecondViewController

.



7) In the Storyboard, drag (hold down the key and drag) from the button in the first one ViewController

to the new one SecondViewController

. Select Show As Action from the pop-up menu.

8) Drag the shortcut and drop it into SecondViewController

. Add IBOutlet

to it as you did in step 4 and name 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 method viewDidLoad

:mylabel.text = firstVCtext

12) Run the program!

+2


source


Another good tutorial here with code and images:



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

+1


source


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

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.

0


source







All Articles