user1469734
user1469734

Reputation: 801

UIStepper with Decimals

My Swift app uses the UIStepper object for doing a simple plus and minus for a value, but in the configuration it only accepts a round number as a Step. I want to configure my UIStepper that it makes steps with 0.5. I tried the following on the Value Changed event:

  @IBAction func changedValueOfStepper(_ sender: UIStepper) {
        self.stepper.value = self.stepper.value * 0.5
        testLabel.text = String(self.stepper.value)
  }

But that doesn't work correctly. Should I do this on a different Event or change the code?

Upvotes: 0

Views: 1059

Answers (2)

Khushbu
Khushbu

Reputation: 2430

Write this line in viewDidLoad method.

stepper.stepValue = 0.5

And then replace your code with this code.

@IBAction func changedValueOfStepper(_ sender: UIStepper) {
      testLabel.text = String(self.stepper.value)
}

Upvotes: 4

rbaldwin
rbaldwin

Reputation: 4848

You can specify steps of 0.5 either in Code:

    stepper.value = 0.0
    stepper.stepValue = 0.5 //<-- This
    stepper.minimumValue = 0.0
    stepper.maximumValue = 10.0

Or in Storyboard:

enter image description here

In your function you can just update your label:

    @IBAction func changedValueOfStepper(_ sender: UIStepper) {
        testLabel.text = String(self.stepper.value)
    }

Upvotes: 2

Related Questions