Swift UI special training 41 implements step per value transfer using pure code, uistepper
I have previously discussed how to capture the value of a control through storyboard. Now let's try to implement the same function through code-only. First define a stepper and a label, and use the label to display the current value of stepper.
Self. priceStepper = UIStepper (frame: CGRectMake (150,120,100, 20) 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 dragging the stepper In the storyboard. We added an addTarget to this step, and the object is its own, the step per value is captured. This method is triggered whenever we click + or. After the ValueChanged method is captured, an action: showPrice is called. The showPrice code is as follows:
Func showPrice (sender: UIStepper !) {Self. tasteLabel. text = "maximum consumption: \ (Int (self. priceStepper. value ))"}
TasteLabel is defined previously to display the stepper value. The Code is as follows:
Self. tasteLabel = UILabel (frame: CGRectMake (20,130,120, 10) // self. tasteLabel. textAlignment = NSTextAlignment. center self. tasteLabel. font = UIFont. boldSystemFontOfSize (17) self. tasteLabel. text = "Consumption ceiling: 100" dialogContainer. addSubview (self. tasteLabel)
The running effect is as follows:
After you click stepper, the values in the label will change: