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 You have to declare oldValue as an int (or long/float/NSInteger etc. etc.) in Header File
//So that you can access globally to that particular implementation file
- (void)viewDidLoad
{
[super viewDidLoad];
oldValue=stepperObj.value;
}
- (IBAction)stepperStep:(id)sender
{
if (stepperObj.value>oldValue) {
oldValue=oldValue+1;
//Your Code You Wanted To Perform On Increment
}
else {
oldValue=oldValue-1;
//Your Code You Wanted To Perform On Decrement
}
}
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With