0

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

vin
  • 1,258
  • 5
  • 20
  • 33
  • I have given a similar [answer](http://stackoverflow.com/a/16391738/767730). The difference being value could be changed by both stepper and a slider, here it is a textfield and stepper. – Anupdas Jun 15 '13 at 06:22

4 Answers4

0

try this code:

- (void)viewDidLoad;
    {

        [self.stepper setValue:10];
      // steppr get valye from textfiled

    }

  - (IBAction)stepperChanged:(UIStepper *)sender
    {

             NSUInteger value = sender.value;
             NSLog(@"%d",value);
             NSLog(@"%@",self.XCoordinate.text);

            self.XCoordinate.text = [@(value) stringValue];

            [self.stepper setValue:value];

    }
 -(void)textFieldDidEndEditing:(UITextField *)textField;
{

 [self.stepper setValue: self.XCoordinate.text.doubleValue];

}

hope it help You

kirti Chavda
  • 3,029
  • 2
  • 17
  • 29
  • i used your code,but it starts from 0 and goes on incrementing from there onwards. – vin Jun 15 '13 at 06:19
  • To form a string from a number, it would be more graceful if you use literals. `[@(value) stringValue]`. – Anupdas Jun 15 '13 at 06:26
  • @kirtimali the textfield changes to 1 and increments from there,i had already tried this,it doesnt take the current value,it starts from 1 and increments from there,i want to take the value from the textfield and update that value using stepper. – vin Jun 15 '13 at 06:31
  • @kirtimali e.g i already have a value 212 in my UITextField,now i when i use the increment/decrement the stepper it has to change to 211 or 210,depending on whether i incremented or decremented.It shouldnt start from 0 and increment to 1. – vin Jun 15 '13 at 06:38
  • you set viewdidload or textfieldendediting [self.stepper setValue:yoyrtxt double value]; – kirti Chavda Jun 15 '13 at 06:44
  • @vin issue soleved or not ? – kirti Chavda Jun 15 '13 at 08:53
0

You should not do it in this method. From what I understand, you want to be able to change the text in the UITextfield from the UIStepper and also, if the text in the UITextfield changes, you want to update the Stepper.

You should use the Value Changed event of the UIStepper to update the text field, but when you want to update the actual stepper from the text field, you should have another method.

- (void)updateStepper:(UIStepper *)stepper fromTextField:(UITextField *)textfield {
    NSInteger value = [textfield.text integerValue];
    [stepper setValue:value];
}
Nikola Kirev
  • 3,152
  • 3
  • 22
  • 30
  • so are you saying to call the above method from the UIStepper's value changed method? – vin Jun 15 '13 at 06:35
  • no. you should call this method when the value is changed from the textfield. For example, if you type in new value from the keyboard. – Nikola Kirev Jun 15 '13 at 10:14
0

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

vin
  • 1,258
  • 5
  • 20
  • 33
0

This alternative worked for me. Every time I edit the textfield the stepper.value is set. This way when I tap the stepper it has works with the value it already has, the textfield value or it's own value.

- (IBAction)didTapStepper:(UIStepper *)sender {
    self.textFieldName.text = [NSString stringWithFormat:@"%ld", (long)sender.value];
}

- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string{
    if(textField == self.textFieldName){
        self.stepperName.value = [[NSString stringWithFormat:@"%@%@", self.textFieldName.text, string] doubleValue];
    }
    return YES;
}

EDIT: Don't forget to add the Delegate UITextFieldDelegate, and assign yourself to it.

self.textFieldName.delegate = self;
Armando
  • 75
  • 5