4

How do I possibly know which button(minus or plus button) of the stepper has been clicked by user?

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

thanks :3

vegligi
  • 61
  • 1
  • 5
  • 1
    You could keep a secondary storage of the value represented by the stepper, and then see if it went up or down – Dan F Jul 02 '13 at 19:02
  • thanks for your reply. I wonder if is there any "native" way to solve this. otherwise the program looks not that clean :P – vegligi Jul 02 '13 at 19:06
  • Why do you need to know? – Wain Jul 02 '13 at 19:13
  • It's actually cleaner to have a field backing the value of the stepper. It's not recommended to have your view be the only source of a value in case the view gets unloaded. – ppilone Jul 02 '13 at 19:47
  • lol, just wondering. thanks for the help. :P – vegligi Jul 02 '13 at 21:50

2 Answers2

4

You can, instead of addTarget:action, observe the steppers value property and ask to receive both old and new value in the change dictionary

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

or subclass UIStepper and do the calculation in an overridden -setValue:

Voxar
  • 328
  • 2
  • 10
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
}

}

You have to declare an oldValue as an integer in header file...

Mubin Mall
  • 546
  • 10
  • 25