7

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)

stackich
  • 3,607
  • 3
  • 17
  • 41
M.C.
  • 1,765
  • 3
  • 29
  • 31
  • I don't think there is another way -- at least using any public methods (I would use a property rather than a global variable, but it's basically the same thing). – rdelmar Aug 05 '12 at 04:11

8 Answers8

13

This is simple and shorter way to identify whether "+" clicked Or "-" clicked of UIStepper


//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
        }
}
Mubin Mall
  • 546
  • 10
  • 25
7

First set the minimum and maximum values of the stepper to 0 and 1 and make sure Value Wraps is unchecked.

enter image description here

Then you can use the following code to check which arrow was clicked

-(IBAction)stepperAction:(NSStepper *)sender {
    if ([sender intValue] == 0) {
        NSLog(@"up");
    } else {
        sender.intValue = 0;
        NSLog(@"down");
    }
}
Bob Ueland
  • 1,804
  • 1
  • 15
  • 24
7

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
}
Haider Malik
  • 1,581
  • 1
  • 20
  • 23
6

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
Jeffery Thomas
  • 42,202
  • 8
  • 92
  • 117
  • I am not familiar with wrapped values, but it does seem to do exactly what it should do. Simple and clean. Thank you – M.C. Aug 05 '12 at 20:07
3

Set the minimum to 0, maximum to 2, and the value to 1. Then:

- (IBAction)stepperAction:(UIStepper *)sender // value changed
{
    if ( sender.value == 0) {
        NSLog(@"down");
    } else { // else up
        NSLog(@"up");
    }
    sender.value = 1; // reset
}

If you have more than one stepper, set each tag differently, then still use this method and also test sender.tag.

Jeff
  • 2,659
  • 1
  • 22
  • 41
3
var sampleStepperValueForIncrement:Int = Int()


@IBAction func sampleStepperValueChanged(_ sender: UIStepper) {        

        if(Int(sender.value) > sampleStepperValueForIncrement){
            print("increasing")
            sampleStepperValueForIncrement += 1

        }
        else{
            print("decresing")
            sampleStepperValueForIncrement =  sampleStepperValueForIncrement - 1
        }
    }
Alvin George
  • 14,148
  • 92
  • 64
2

One way is to use the UISteppers tag property. So at viewDidLoad set the tag to the value. From then on in every action method you can first compare then at the end is the method update the value.

David H
  • 40,852
  • 12
  • 92
  • 138
  • does not seem like the correct solution, but it is probably the best solution for now. Maybe there is no correct solution... – M.C. Aug 05 '12 at 13:22
  • Well, the issue is you want to have some data tagged to this control. What I gave you was a solution. Otherways would be to subclass the control - ie MyStepper, and add one or more properties to the subclass. Categories cannot define properties now so cannot go down that road. You could have a mutableDictionary ivar, and use the control as the key and a NSNumber as the value, and you could support many such associations in one class. Also, you had said you were using a global before, but he most common solution would be touse an ivar (ie NSInteger oldVal;) [PS thanks for giving me the check!] – David H Aug 05 '12 at 13:26
1

Swift 2.0:

Declarations:

@IBOutlet weak var mapViewZoomStepper: UIStepper!
 var mapViewZoomStepperValue: Double = -1.0

When value changes :

    @IBAction func mapViewZoomStepperValueChanged(sender: AnyObject) {        

     if (mapViewZoomStepper.value  > mapViewZoomStepperValue)
      {
       print("increment")
       mapViewZoomStepperValue = mapViewZoomStepperValue + 1.0

      }
      else
      {
       print("decrement")
       mapViewZoomStepperValue = mapViewZoomStepper.value - 1.0 
      }

  print("compare //  stored Value :   \(mapViewZoomStepperValue)  && real time value : \(mapViewZoomStepper.value)")

     }

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:

switch (mapViewZoomStepper.value) {
  case 0:
    print("A")
    break;
case 1:
 print("B")
 break;

  default:
    break;
}
Alvin George
  • 14,148
  • 92
  • 64