Swift UI special training 41 implements step per value transfer using pure code, uistepper

Source: Internet
Author: User

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:




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.