How to use UIStepper

Mc.Lover picture Mc.Lover · Oct 15, 2011 · Viewed 68.5k times · Source

I am trying to work with UIStepper to increment or decrement an integer, but both "-" and "+" increase the integer! How can I recognize the "+" and "-" button?

In the UIStepper header file there are two UIButtons:

UIButton *_plusButton;
UIButton *_minusButton;

for example :

- (IBAction)changeValue:(id)sender 
{        
    UIStepper *stepper = (UIStepper *) sender;

    stepper.maximumValue = 10;
    stepper.minimumValue = 0;      
    if (stepper)
    {
        integer++;
        [label setText:[NSString stringWithFormat:@"%d",integer]];
     }
     else
     { 
         integer--;
         [label setText:[NSString stringWithFormat:@"%d",integer]];
     }

}

Answer

Dave DeLong picture Dave DeLong · Oct 15, 2011

You should ignore the ivars. They will not help you.

The UIStepper has a value property that you can query to figure out what the current value is. So your method could simply be:

- (IBAction)valueChanged:(UIStepper *)sender {
  double value = [sender value];

  [label setText:[NSString stringWithFormat:@"%d", (int)value]];
}