how to get values from UITextField to another textfield

I get 3 inputs from users from 3 different UITEXTFIELDS,

need to add them and show the result on different uitexfields but without pressing a button. i need it to be done automatically.

Anyone could help ?

Replies

All the UiTextFields are in the same view ?

How are the UITextFields modified (on user entry ? from notification ?)

Have you a resultTextField ?


So, assuming they are:


@IBOutlet var textField1: UITextField!
@IBOutlet var textField2: UITextField!
@IBOutlet var textField3: UITextField!
@IBOutlet var resultField: UITextField!


Then connect each textField Editing Changed event to this IBAction:

     @IBAction func editingTestField(_ sender: UITextField) {
          let resultText = textField1.text! + " - " + textField2.text! + " - " + textField3.text!
          resultField.text = resultText
}

You can use the textfield's delegate function shouldChangeCharactersInRange:


https://developer.apple.com/documentation/uikit/uitextfielddelegate/1619599-textfield?language=objc


It is called whenever the user wants to make a change in the textfield. After you collect the 3 textfield contents and display it as you wish, just return YES.


Note that you will need to "change characters in range" for one textfield yourself because it will not have been done at that time. Alternatively you could do a

[self performSelector:@selector(collectTextFields) withObject:nil afterDelay:0.1f];

within that method and in "collectTextFields" you would collect all the textfields (including the one that was changed within that 0.1 second delay).