UIStepper. Find out whether incremented or decremented

Viewed 13304

Figuring out whether the plus or minus button was pressed in UIStepper I use this method:

- (void)stepperOneChanged:(UIStepper*)stepperOne

And I compare stepperOne.value with a global value saved in my TableView Class.
I dont think this is the right way.

So to clarify i will show the "bad" code i am using:

- (void)stepperOneChanged:(UIStepper*)stepperOne
{
      BOOL PlusButtonPressed=NO;  
      if(stepperOne.value>globalValue)  
      {   
          PlusButtonPressed =YES;  
      }  
      globalValue=stepperOne.value;

    ////do what you need to do with the PlusButtonPressed boolean
}

So what is the right way to do this? (without having to save global variables)

9 Answers

Swift 3.0:

In your stepper action do make sure you reset stepper.value to 0 after, this makes the stepper value -1 on negative press and 1 on positive press.

@IBAction func Stepper(_ sender: UIStepper) {
    if sender.value == 1.0{
        //positive side was pressed
    }else if sender.value == -1.0{
        //negative side was pressed
    }
    sender.value = 0  //resetting the stepper value so negative is -1 and positive is 1
}

Swift 5.0

var oldValue: Int!

override func viewDidLoad(){
    super.viewDidLoad()
    oldValue = Int(stepper.minimumValue)
}

@IBAction(){
if Int(sender.value) > oldValue{
  // Positive
}else{
   // Negative
}
oldValue = Int(sender.value)

 }
Related