Having previously talked about capturing the value of a control by storyboard, let's try to implement the same functionality in a pure code way. First define a stepper and a label, and use the label to display the current value of the stepper.
Self.pricestepper = Uistepper (Frame:cgrectmake ( min, max )) Self.priceStepper.minimumValue = 100//Minimum Self.priceStepper.maximumValue = 2000//maximum self.priceStepper.stepValue = 100//Step Self.priceStepper.tintColor = Uicolor.orangecolor () self.priceStepper.addTarget (Self, Action: "Showprice:", forControlEvents:UIControlEvents.ValueChanged) Dialogcontainer.addsubview (Pricestepper)
We used the Addtarget method, which is the same as the valuechanged in the action of the previous drag stepper in storyboard, and we added a addtarget to the stepper, which is the object itself, The catch is the change of the stepper value, which is triggered whenever we click + or-. A action:showprice is called after the ValueChanged method has captured the. The code for Showprice is as follows:
Func Showprice (sender:uistepper!) { Self.tasteLabel.text = "Consumption limit: \ (Int (Self.priceStepper.value))" }
Tastelabel is the value we defined previously to display stepper, the code is as follows:
Self.tastelabel = UILabel (Frame:cgrectmake (+, +, +)) //self.tasteLabel.textAlignment = Nstextalignment.center Self.tasteLabel.font = uifont.boldsystemfontofsize (+) Self.tasteLabel.text = "Consumption limit : " Dialogcontainer.addsubview" (Self.tastelabel)
The operating effect is as follows, the initial state:
When you click Stepper, the value in the label changes:
Swift UI special training 41 implementing stepper value delivery in a pure code manner