Question

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

Was it helpful?

Solution 3

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

OTHER TIPS

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

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

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;
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top