4

我怎么可能知道用户点击了步进器的哪个按钮(减号或加号按钮)?

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

谢谢:3

4

2 回答 2

4

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

{
    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 中进行计算:

于 2013-10-24T18:50:34.087 回答
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 声明为整数...

于 2014-04-07T21:09:32.180 回答