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)
First set the minimum and maximum values of the stepper to 0 and 1 and make sure Value Wraps is unchecked.
Then you can use the following code to check which arrow was clicked
Set the minimum to 0, maximum to 2, and the value to 1. Then:
If you have more than one stepper, set each tag differently, then still use this method and also test sender.tag.
Swift 2.0:
Declarations:
When value changes :
Another option is to limit maximum value to 1. So the uistepper will have two state - 0 and 1 .
Use a switch statement to differentiate:
This is simple and shorter way to identify whether "+" clicked Or "-" clicked of UIStepper
Swift 3.0:
This worked for me:
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.