Swift UI special training 41 implementing stepper value delivery in a pure code manner

Source: Internet
Author: User

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

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.