UIStepper。找出是递增还是递减 [英] UIStepper. Find out whether incremented or decremented

查看:158
本文介绍了UIStepper。找出是递增还是递减的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

Figuring out whether the plus or minus button was pressed in UIStepper I use this method:

- (void)stepperOneChanged:(UIStepper*)stepperOne

我将stepperOne.value与保存在TableView类中的全局值进行比较。

我不认为这是正确的方法。

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
}


$做你需要做的事情b $ b

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

So what is the right way to do this? (without having to save global variables)

推荐答案

所以我想到了这个的子类。事实证明并非如此糟糕(包装值除外)。

So I thought about a subclass for this. It turns out to be not so bad (except for wrapped values).

使用子类

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

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

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

这篇关于UIStepper。找出是递增还是递减的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆