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)
So I thought about a subclass for this. It turns out to be not so bad (except for wrapped values).
Using the subclass
- (IBAction)stepperOneChanged:(UIStepper*)stepperOne
{
if (stepperOne.plusMinusState == JLTStepperPlus) {
// Plus button pressed
}
else if (stepperOne.plusMinusState == JLTStepperMinus) {
// Minus button pressed
} else {
// Shouldn't happen unless value is set programmatically.
}
}
JLTStepper.h
#import <UIKit/UIKit.h>
typedef enum JLTStepperPlusMinusState_ {
JLTStepperMinus = -1,
JLTStepperPlus = 1,
JLTStepperUnset = 0
} JLTStepperPlusMinusState;
@interface JLTStepper : UIStepper
@property (nonatomic) JLTStepperPlusMinusState plusMinusState;
@end
JLTStepper.m
#import "JLTStepper.h"
@implementation JLTStepper
- (void)setValue:(double)value
{
BOOL isPlus = self.value < value;
BOOL isMinus = self.value > value;
if (self.wraps) { // Handing wrapped values is tricky
if (self.value > self.maximumValue - self.stepValue) {
isPlus = value < self.minimumValue + self.stepValue;
isMinus = isMinus && !isPlus;
} else if (self.value < self.minimumValue + self.stepValue) {
isMinus = value > self.maximumValue - self.stepValue;
isPlus = isPlus && !isMinus;
}
}
if (isPlus)
self.plusMinusState = JLTStepperPlus;
else if (isMinus)
self.plusMinusState = JLTStepperMinus;
[super setValue:value];
}
@end