UIStepper. Find out whether incremented or decremented

六月ゝ 毕业季﹏ 提交于 2019-11-30 21:07:07

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

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
        }
}

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

-(IBAction)stepperAction:(NSStepper *)sender {
    if ([sender intValue] == 0) {
        NSLog(@"up");
    } else {
        sender.intValue = 0;
        NSLog(@"down");
    }
}

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
}

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.

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.

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;
}
var sampleStepperValueForIncrement:Int = Int()


@IBAction func sampleStepperValueChanged(_ sender: UIStepper) {        

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

        }
        else{
            print("decresing")
            sampleStepperValueForIncrement =  sampleStepperValueForIncrement - 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)

 }
标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!