Home | Contact Us

CSharp | Java | Python | Swift | GO | WPF | Ruby | Scala | F# | JavaScript

<< Back to SWIFT

Swift UIStepper Usage

Use a UIStepper to increment and decrement numbers with a control. Access the Stepper's value.
UIStepper. Our values are 0, 1, 2. With a UIStepper we provide a numeric increment and decrement control. A number can be increased or decreased.
Some notes. When the user taps on a Stepper, the "value" property of the Stepper goes from 1.0 to 2.0 to 3.0. In our tutorial we add a Label. This is a good place to display values.
Let us start. Please drag a Stepper and a Label to your Single View Application window in Xcode. These two controls will be used in our tutorial.
In iPhone development we add outlets to controls. This allows us to reference controls (like Stepper) in Swift code. Control-drag the Stepper and Button to the ViewController file.

Name: I chose the names simpleStepper and simpleLabel just to keep things simple for this tutorial.

ActionTriggered: Right-click on the Stepper and drag the "Action Triggered" circle to the ViewController to create a method.

Here: We add code to the actionTriggered func. We read in the value and set the text to a string that includes the value.

Example UIStepper usage: Swift // // ViewController.swift // ExampleTimeF // // ... // import UIKit class ViewController: UIViewController { @IBOutlet weak var simpleStepper: UIStepper! @IBOutlet weak var simpleLabel: UILabel! override func viewDidLoad() { super.viewDidLoad() } @IBAction func actionTriggered(sender: AnyObject) { // Get value of UIStepper. // ... This is a double. let value = simpleStepper.value // Set text of label to the value of the stepper. simpleLabel.text = "Value: " + String(value) } override func didReceiveMemoryWarning() { super.didReceiveMemoryWarning() } }
Value property. The UIStepper provides a handy value property. This is a double, but we can cast it to an Int without much trouble.Int
Run simulator. Let us try our iOS program. When we run the simulator, we click on the Stepper (this is the same as tapping). The value goes up and down as we click.
A review. The UIStepper is a great way to increment or decrement a small numeric value. For large numbers, or numbers where large adjustments are needed, UIStepper is not ideal.
The Dev Codes