UIStepper。找出是递增还是递减

问题描述 投票:5回答:8

确定在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
}

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

ios uistepper
8个回答
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

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

5
投票

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

enter image description here

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

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

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。


3
投票

Swift 3.0:

在你的步进操作中确保你将stepper.value重置为0之后,这使得步进值-1在负按下时为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
}

2
投票

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


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

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
        }
    }
© www.soinside.com 2019 - 2024. All rights reserved.