SwiftUI from entry to actual combat Chapter 2 Section 4: Stepper

Related courses: http://hdjc8.com/hdjc/swiftUI/

Stepper stepper is equivalent to UIStepper in UIKit, which is used to increase and decrease data.

This section demonstrates the use of Stepper stepper. The stepper consists of an increase button, a decrease button and a value label, which is very suitable for precise adjustment of values ​​in a small range.


Sample code:

struct ContentView : View {
    
     //首先添加一个浮点类型的属性,并设置它的初始值为0。该属性拥有@State绑定包装标记,表示该属性将和步进器视图进行数据绑定。
     @State var temperature: Double = 0

       var body: some View {
        
           VStack {
                //添加一个步进器视图,接着将依次设置步进器的递增、递减事件和数值标签。
                Stepper(onIncrement: {
                    self.temperature += 1
                }, onDecrement: {
                    self.temperature -= 1
                }, label: { Text("Temperature: \(Int(temperature))") })
                
                Stepper(onIncrement: {
                    self.temperature += 1
                }, onDecrement: {
                    self.temperature -= 1
                }, onEditingChanged: { (item) in
                    print(item)
                }, label: { Text("Temperature: \(Int(temperature))") })
           
       }.padding()
    }
}

Show running results:

Guess you like

Origin blog.csdn.net/fzhlee/article/details/106083861