C-Sharp | Java | Python | Swift | GO | WPF | Ruby | Scala | F# | JavaScript | SQL | PHP | Angular | HTML
Steps: As an overview, we create a Label and then add an Outlet to the Label in ViewController. Then we change the Label's text in Swift.
So: With the approach here, we can use Swift code to change the values of controls in an iPhone program. Many things are possible.
Tip: We must add controls to Main.storyboard from the object library in Xcode. Continue with the next steps.
Then: Click the object library button. Now scroll down through the controls we can add. Stop when you see "Label."
Click: Next click the Assistant editor icon (which has weird circles on it). This splits the screen—ideal for dragging things.
Important: Your window should now have the iPhone interface (with the label) on the left and the ViewController.swift file on the right.
Note: Blue lines will appear. The text message "Create Outlet, Action, or Outlet Collection" will also appear.
Drop: Release the drag and a box will appear. Type the name of your Label—I prefer the name "simpleLabel." Click Connect.
Quote: Outlets provide a way to reference interface objects—the objects you added to your storyboard—from source code files.
Connect the UI to Code: apple.comExample ViewController.swift before: Swift
//
// ViewController.swift
// ExampleTime
//
// ...
//
import UIKit
class ViewController: UIViewController {
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
}
IBOutlet: This is the Field reference. Its name is simpleLabel. The keyword "weak" describes its allocation model.
UILabel: You can see the type of the Label reference is a UILabel. We must access the Label as a UILabel in Swift.
Example ViewController.swift after: Swift
//
// ViewController.swift
// ExampleTime
//
// ...
//
import UIKit
class ViewController: UIViewController {
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
}
@IBOutlet weak var simpleLabel: UILabel!
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
}
Result: When the app runs, the text of the Label will be set to "ABC" by our Swift code.
So: We now have a program that runs on the iPhone and executes Swift code to change its initial state.
Example ViewController.swift, with label update: Swift
//
// ViewController.swift
// ExampleTime
//
// ...
//
import UIKit
class ViewController: UIViewController {
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
// ... Change label text.
simpleLabel.text = "ABC"
}
@IBOutlet weak var simpleLabel: UILabel!
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
}
Finally: Ensure the text "ABC" from your Label appears on the iPhone simulator. If it does, you have a functioning Swift iOS program.