UIStepper. Find out whether incremented or decremented

M.C. picture M.C. · Aug 5, 2012 · Viewed 12.2k times · Source

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

- (void)stepperOneChanged:(UIStepper*)stepperOne

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
}

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

Answer

Mubin Mall picture Mubin Mall · Apr 7, 2014

This is simple and shorter way to identify whether "+" clicked Or "-" clicked of UIStepper


//First You have to declare oldValue as an int (or long/float/NSInteger etc. etc.) in Header File 
//So that you can access globally to that particular implementation file

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

- (IBAction)stepperStep:(id)sender 
{
        if (stepperObj.value>oldValue) {
             oldValue=oldValue+1;
             //Your Code You Wanted To Perform On Increment
        }
       else {
             oldValue=oldValue-1;
             //Your Code You Wanted To Perform On Decrement
        }
}