j-s
j-s

Reputation: 157

UITextField change in iOS for conversions

I am creating a conversion application. I have multiple text fields. What I intend to do is when I start entering Kg value its equivalent gram and pound value should be displayed in the gram and pound text fields. How can it be done using editingChanged function in iOS?

Upvotes: 0

Views: 52

Answers (1)

sundance
sundance

Reputation: 3030

This question is very broad but I will try to give a short answer of how to track changed of text fields:

class YourViewController: UIViewController {

    @IBOutlet weak var textField: UITextField!

    @IBAction func textFieldDidChange(_ sender: UITextField) {
        self.textField.text = doSomeCalculation(sender.text)
    }

}

First, you need to set up a view controller (or a view or whatever suits your interface design) with a UITextField. Then you connect the UITextField with the variable textField and the desired action of the text field with the function textFieldDidChange. You can use the action Editing Changed but I prefer Value Changed (depends on what you want to achieve).

Any time the text field is changed the function textFieldDidChange is called and you can do your calculations and alter the text of textField.

Upvotes: 1

Related Questions