0

I'm using xCode 4.6, and i have a little problem where. In my app I have 2 text fields one says VG and the other says PG (the VG and PG are always equal 100). The user would be able to put on each text field a percentage amount, now lets say a user inputs on the VG text field a number like 10, then automatically the PG would change whatever number it has to the correct amount to equal 100. It calculates well from VG being the one with the input and PG showing the result, but if I try to do it the other way around, it doesn’t work. If the user wants to change the value from PG and get result on the VG text field, it will show the calculation result from VG being the input. So basically I would like to make the calculation go both ways. I have it set up with floats.

-(void)textfieldShoudEndEditing (UITextField *)textfield {
Float A = [VG.text floatvalue];
Float B = [PG.text floatvalue];
Float result1 = (100 – A)
PG.text = [NSString stringwithformat:@”%.0f”, result1];
Floar result2 = (100 – B)
VG.text 0 [NSString stringwithformat:@”%.0f”, result2];
}

thank you

Firoze Lafeer
  • 17,133
  • 4
  • 54
  • 48

2 Answers2

0

You need to test the textField parameter to see which one is being edited.

Something like:

if (textfield == PG) {
    //update VG
}
else if (textfield == VG) {
    //update PG
}
else {
    //if you ever have other textfields and want to do something
}
Lance
  • 8,872
  • 2
  • 36
  • 47
0
- (void)textFieldDidEndEditing:(UITextField *)textField
{
    if (textField == VG)
    {
        PG.text = [NSString stringWithFormat:@"%.0f", 100.0f - [textField.text floatValue]];
    }
    else if (textField == PG)
    {
        VG.text = [NSString stringWithFormat:@"%.0f", 100.0f - [textField.text floatValue]];
    }
}

You can use the textField parameter that's passed into the delegate method to determine the correct text field to update.

Here we are just updating the text field that hasn't just finished editing.

Jason Cabot
  • 151
  • 4
  • This is just what I was looking for, I tried using if statements but I wasn't checking if the text field was being used. Thank you for the help – David Castro Aug 13 '13 at 00:36