C-Sharp | Java | Python | Swift | GO | WPF | Ruby | Scala | F# | JavaScript | SQL | PHP | Angular | HTML
Tip: You may need to open the Assistant editor to split your screen. The icon is in the top right.
Xcode: Assistant Editor TipsName: Please name the Text Field something simple like "simpleText." This sounds pretty simple to me.
Sent events: Now locate the "Sent Events" section in this pop-up window. Small circles are on the right side.
Drag: Click and drag the small circle next to "Editing Changed" to the ViewController text file. This creates an event handler.
Next: We should have a changedText func in the ViewController. Make sure both the Label and Text Field have outlets.
If let: We use optional binding to get the value from the UITextField's text property. We then use this string to set the Label's text.
OptionalStringsSwift program that uses UITextField
//
// ViewController.swift
// ExampleTime2
//
// ...
//
import UIKit
class ViewController: UIViewController {
override func viewDidLoad() {
super.viewDidLoad()
}
@IBAction func changedText(sender: AnyObject) {
// Use "if let" to get string from text field.
if let value = simpleText.text {
// Uppercase string.
// ... Use it as the label text.
simpleLabel.text = value.uppercaseString
}
}
@IBOutlet weak var simpleText: UITextField!
@IBOutlet weak var simpleLabel: UILabel!
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
}
}
And: As you type, the Label should update with an uppercased version of what you just typed. If this happens, the program worked.