Question

I'm building an application that transforms millimeter, centimeters, decimeters, meters, etc... For this, I put a bunch of textFields on my ViewController and I want them to auto-reload and calculate the correct answer without making the use of a button.
Example, the user leaves the millimeters textField with 200 in it, I want the centimeters textField to update itself to 20 (= milliTextField / 100).

I have tried a bunch of method I found online but my ViewController is just staying frozen...

Also, I know that an input in a textField is a string, so I used the method below to change it into a float, calculate and put it back into the textField as a string... but I have no certitude the code is really doing what I expect it to do...

-(void)abaqueBrain
{
    if (_milliTextField != nil)
    {
    float centiValue = [self.milliTextField.text floatValue] / 10.0;
    self.centiTextField.text = [NSString stringWithFormat:@"%2f", centiValue];
    [UIView commitAnimations];
        // etc... (manage the other textFields higher than centimeters)
    }
    else if (_centiTextField !=nil)
    {
        // etc...
    }
}
Was it helpful?

Solution

What you need is UITextFieldDelegate; you want to respond to user as he/she types in the textfield.

The UITextFieldDelegate provides the following methods:

1. textFieldShouldBeginEditing:
2. textFieldDidBeginEditing:
3. textFieldShouldEndEditing:
4. textFieldDidEndEditing:

You need to conform to this protocol. When conformed, you'll receive the above mentioned call-back methods to updated the other Views in your app.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top