C-Sharp | Java | Python | Swift | GO | WPF | Ruby | Scala | F# | JavaScript | SQL | PHP | Angular | HTML
Name: Provide names for the UISlider and UILabel—I used "simpleSlider" and "simpleLabel" to stress how simple I hope they will be.
Example ViewController: Swift
//
// ViewController.swift
// ExampleTime9
//
// ...
//
import UIKit
class ViewController: UIViewController {
override func viewDidLoad() {
super.viewDidLoad()
}
@IBOutlet weak var simpleSlider: UISlider!
@IBOutlet weak var simpleLabel: UILabel!
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
}
}
Tip: To add an event handler for "Primary Action Triggered," drag from the adjacent circle to the ViewController file.
Next: Please add some code to the event handler (named "sliderMoved" here). Here we use the UILabel and the UISlider together.
Example ViewController with IBAction: Swift
//
// ViewController.swift
// ExampleTime9
//
// ...
//
import UIKit
class ViewController: UIViewController {
override func viewDidLoad() {
super.viewDidLoad()
}
@IBAction func sliderMoved(sender: AnyObject) {
// Get Float value from Slider when it is moved.
let value = simpleSlider.value
// Assign text to string representation of float.
simpleLabel.text = String(value)
}
@IBOutlet weak var simpleSlider: UISlider!
@IBOutlet weak var simpleLabel: UILabel!
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
}
}
Important: When learning new platforms, we must focus on how things work, not whether they are useful or beautiful or not.