UIStepper。找出是递增还是递减

时间:2012-08-05 03:50:59

标签: ios uistepper

确定在UIStepper中是否按下加号或减号按钮我使用此方法:

- (void)stepperOneChanged:(UIStepper*)stepperOne

我将stepperOne.value与保存在TableView类中的全局值进行比较   我不认为这是正确的方法。

因此,为了澄清我将显示我正在使用的“坏”代码:

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

那么这样做的正确方法是什么? (无需保存全局变量)

9 个答案:

答案 0 :(得分:11)


这是识别" +"的简单方法。点击或" - "点击了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
        }
}

答案 1 :(得分:6)

所以我想到了这个的子类。事实证明并不是那么糟糕(包装值除外)。

使用子类

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

答案 2 :(得分:5)

首先将步进器的最小值和最大值设置为0和1,并确保未选中Value Wraps。

enter image description here

然后,您可以使用以下代码检查单击了哪个箭头

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

答案 3 :(得分:3)

将最小值设置为0,将最大值设置为2,将值设置为1.然后:

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

如果您有多个步进器,请以不同方式设置每个标签,然后仍然使用此方法并测试sender.tag。

答案 4 :(得分:3)

Swift 3.0

在步进操作中,请确保在将stepper.value重置为0之后,这使步进值在负压下为-1,在正压下为1。

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

答案 5 :(得分:2)

一种方法是使用UISteppers标记属性。所以在viewDidLoad中将标记设置为值。从那时起,您可以先在每个操作方法中进行比较,然后在最后更新方法。

答案 6 :(得分:1)

Swift 2.0:

声明:

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

价值变化时:

    @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)")

     }

另一种选择是将最大值限制为1.因此uistepper将有两个状态 - 0和1。

使用switch语句区分:

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

  default:
    break;
}

答案 7 :(得分:1)

var sampleStepperValueForIncrement:Int = Int()


@IBAction func sampleStepperValueChanged(_ sender: UIStepper) {        

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

        }
        else{
            print("decresing")
            sampleStepperValueForIncrement =  sampleStepperValueForIncrement - 1
        }
    }

答案 8 :(得分:-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)

 }