Stepper And Slider
Part of the Introduction to SwiftUI section of Coddy's Swift journey. Lesson 26 of 50.
A Stepper has - and + buttons that change a number. The in: range keeps the value inside limits:
@State private var cups = 1
Stepper("Cups: \(cups)", value: $cups, in: 1...5)At 5 the + button stops working, and at 1 the - button does. step: changes by more than one at a time.
A Slider picks a Double by dragging:
@State private var volume = 50.0
Slider(value: $volume, in: 0...100, step: 5)The state is a Double, so write 50.0, not 50, when you declare it.
Challenge
EasyLet the user choose how many tickets to buy. Replace the text with a Stepper titled Tickets: 1, Tickets: 2, ... bound to tickets, from 1 to 4. Give the stepper the identifier tickets.
Try it yourself
import SwiftUI
@main
struct CoddyApp: App {
var body: some Scene {
WindowGroup {
ContentView()
}
}
}This lesson includes a short quiz. Start the lesson to answer it and track your progress.
All lessons in Introduction to SwiftUI
Practice on your own: Swift playground