0

How can I use UIStepper as simple increaser/decreaser?

The only way I've found is to set the value of the UIStepper to 1 each time I detect an action and compare the value to 1 when I catch an action:

    @IBAction func onStepperValueChangedAction(sender: UIStepper) {
        if sender.value > 1 {
            increaseValue()
        } else {
            decreaseValue()
        }

        sender.value = 1
    }

Don't forget to set the default value of the UIStepper to 1.

Jojo56400
  • 309
  • 3
  • 12

4 Answers4

1
@IBAction func StepperPressed(_ sender: UIStepper) {

    Label.text = Int(sender.value).description

}
Yaswanth
  • 11
  • 2
1
@IBOutlet weak var splitNumberLabel: UILabel!

@IBAction func stepperValueChanged(_ sender: UIStepper) {
    splitNumberLabel.text = String(sender.value)
}
Tyler2P
  • 2,324
  • 26
  • 22
  • 31
anusha.V
  • 71
  • 2
  • 1
    Your answer could be improved with additional supporting information. Please [edit] to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers [in the help center](/help/how-to-ask). – Community Sep 21 '21 at 19:11
0

Try this

var previousValue = 0
     @IBAction func onStepperValueChangedAction(sender: UIStepper) {

            if sender.value > previousValue {
                increaseValue()
            } else {
                decreaseValue()
            }
            previousValue = sender.value
        }
Alexander B
  • 137
  • 2
  • 12
  • This will not work if I reach the maximum value. Example: If the maximum value of the stepper is 10, the value is 8 and I use the increase button 3 times, only the 2 first actions will be taken into consideration. The third one will be interpreted as a decrease. – Jojo56400 Jul 07 '16 at 10:31
  • if sender.value >= sender.maximumValue return – Alexander B Jul 07 '16 at 10:37
  • Still, the 3rd try will not work. It's better because it will not be interpreted as a decrease but in this case it will not be interpreted at all. I think it's better to reset the stepper value each time like I did. – Jojo56400 Jul 07 '16 at 10:45
0

You can try this, if it suits your need

    if sender.value >  prevCnt {

        increasedValue()
        sender.maximumValue = sender.maximumValue + 1
    }
    else if prevCnt >= 1 {

        decreasedValue()
        sender.maximumValue = sender.maximumValue - 1
    }
ADK
  • 139
  • 10