Search code examples
iosobjective-cuitextfielduistepper

how to increment or decrement textfield value using uistepper?


I currently have a UITextField which has some value in it and also i have a UIStepper besides it. I want to know how to use the UIStepper so as increment/decrement the textfield value.

CODE:

the value changed action of the UIStepper

1st I tried this

- (IBAction)changedX:(UIStepper*)sender 
{
double value = [sender value];
self.XStepper.value=[self.XCoordinate.text doubleValue];
self.XCoordinate.text=[NSString stringWithFormat:@"%g",value];
NSLog(@"%f",value);
}

then i changed it to this:

if ([sender value]==1) {

   double temp= [self.XCoordinate.text doubleValue];
    temp=temp+1;
       self.XCoordinate.text=[NSString stringWithFormat:@"%g",temp];

}

else
{
    double temp= [self.XCoordinate.text doubleValue];
    temp=temp-1;
    self.XCoordinate.text=[NSString stringWithFormat:@"%g",temp];

}

i am not understanding how do i take the current UITextField value and update it in UIStepper so that current value should be incremented/decremented accordingly.

Thanks


Solution

  • Set the stepper.value to the default value in viewDidAppear/viewDidLoad