Initializer

init(onIncrement:onDecrement:onEditingChanged:label:)

Creates a stepper instance that performs the closures you provide when the user increments or decrements the stepper.

Declaration 宣言

init(onIncrement: (() -> Void)?, onDecrement: (() -> Void)?, onEditingChanged: @escaping (Bool) -> Void = { _ in }, label: () -> Label)
Available when Label conforms to View. LabelViewに準拠する場合に利用可能です。

Parameters パラメータ

onIncrement

The closure to execute when the user clicks or taps the control’s plus button.

onDecrement

The closure to execute when the user clicks or taps the control’s minus button.

onEditingChanged

A closure called when editing begins and ends. For example, on iOS, the user may touch and hold the increment or decrement buttons on a Stepper which causes the execution of the onEditingChanged closure at the start and end of the gesture.

label

A view describing the purpose of this stepper.

Discussion 議論

Use this initializer to create a control with a custom title that executes closures you provide when the user clicks or taps the stepper’s increment or decrement buttons.

The example below uses an array that holds a number of Color values, a local state variable, value, to set the control’s background color, and title label. When the user clicks or taps on the stepper’s increment or decrement buttons SwiftUI executes the relevant closure that updates value, wrapping the value to prevent overflow. SwiftUI then re-renders the view, updating the text and background color to match the current index:


struct StepperView: View {
    @State private var value = 0
    let colors: [Color] = [.orange, .red, .gray, .blue, .green,
                           .purple, .pink]


    func incrementStep() {
        value += 1
        if value >= colors.count { value = 0 }
    }


    func decrementStep() {
        value -= 1
        if value < 0 { value = colors.count - 1 }
    }


    var body: some View {
        Stepper(onIncrement: incrementStep,
            onDecrement: decrementStep) {
            Text("Value: \(value) Color: \(colors[value].description)")
        }
        .padding(5)
        .background(colors[value])
    }

}

A view displaying a stepper that uses a text view for stepper’s title

See Also 参照

Deprecated Initializers