0
0
iOS Swiftmobile~5 mins

Stepper in iOS Swift

Choose your learning style9 modes available
Introduction

A Stepper lets users increase or decrease a value by tapping plus or minus buttons. It is simple and clear for choosing numbers.

Choosing quantity of items to buy in a shopping app.
Setting volume or brightness levels in settings.
Selecting number of guests for a reservation.
Adjusting timer or countdown values.
Changing font size in a reading app.
Syntax
iOS Swift
let stepper = UIStepper()
stepper.minimumValue = 0
stepper.maximumValue = 10
stepper.stepValue = 1
stepper.value = 0
stepper.addTarget(self, action: #selector(stepperChanged(_:)), for: .valueChanged)

Use minimumValue and maximumValue to limit the range.

Use stepValue to set how much the value changes per tap.

Examples
Stepper that goes from 1 to 5, increasing or decreasing by 1.
iOS Swift
let stepper = UIStepper()
stepper.minimumValue = 1
stepper.maximumValue = 5
stepper.stepValue = 1
stepper.value = 1
Stepper that changes in steps of 10, starting at 50.
iOS Swift
let stepper = UIStepper()
stepper.minimumValue = 0
stepper.maximumValue = 100
stepper.stepValue = 10
stepper.value = 50
Sample App

This app shows a label and a stepper. When you tap plus or minus on the stepper, the label updates to show the current value.

iOS Swift
import UIKit

class ViewController: UIViewController {
    let stepper = UIStepper()
    let label = UILabel()

    override func viewDidLoad() {
        super.viewDidLoad()
        view.backgroundColor = .white

        label.text = "Value: 0"
        label.textAlignment = .center
        label.frame = CGRect(x: 50, y: 150, width: 220, height: 40)
        view.addSubview(label)

        stepper.frame = CGRect(x: 50, y: 200, width: 94, height: 29)
        stepper.minimumValue = 0
        stepper.maximumValue = 10
        stepper.stepValue = 1
        stepper.value = 0
        stepper.addTarget(self, action: #selector(stepperChanged(_:)), for: .valueChanged)
        view.addSubview(stepper)
    }

    @objc func stepperChanged(_ sender: UIStepper) {
        label.text = "Value: \(Int(sender.value))"
    }
}
OutputSuccess
Important Notes

Always set minimumValue and maximumValue to avoid unexpected values.

Use addTarget(_:action:for:) to respond when the user changes the stepper.

Stepper values are Double, but you often convert to Int for display.

Summary

Stepper lets users pick a number by tapping plus or minus buttons.

Set minimum, maximum, and step values to control allowed numbers.

Update your UI when the stepper value changes to show the current number.