UIStepper:如何知道用户点击了步进器的哪个按钮(减号或加号按钮)

时间:2013-07-02 19:00:43

标签: ios uistepper

我如何知道用户点击了步进器的哪个按钮(减号或加号按钮)?

- (IBAction)buttonStepper:(id)sender {
    int stepperValue = self.outletStepper.value;
    self.label.text = [NSString stringWithFormat:@"%d", stepperValue];
}

谢谢:3

2 个答案:

答案 0 :(得分:4)

您可以代替addTarget:action,观察steppers值属性并要求在更改字典中接收旧值和新值

{
    UIStepper *stepper = ...;
    [stepper addObserver:self forKeyPath:@"value"
                 options:NSKeyValueObservingOptionOld | NSKeyValueObservingOptionNew
                 context:0];
}

- (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary *)change context:(void *)context
{
    if (object == stepper) {
        double oldValue = change[NSKeyValueChangeOldKey];
        double newValue = change[NSKeyValueChangeNewKey];
        double change = newValue - oldValue;
    }
}

或UIStepper的子类,并在重写的-setValue:

中进行计算

答案 1 :(得分:0)

- (void)viewDidLoad
{
     [super viewDidLoad];
     oldValue=stepperObj.value;
}

- (IBAction)stepperStep:(id)sender {

if (stepperObj.value>oldValue) {

    oldValue=oldValue+1;
    NSLog(@"%d",oldValue);
    //your code do you want to perform on increment
}
else
{
    oldValue=oldValue-1;
    NSLog(@"%d",oldValue);
    //your code do you want to perform on decrement
}

}

您必须在头文件中声明oldValue为整数...