Return Value in UITextField

Hi There,


I am new to swift / xcode and am trying to create a simple equation calculator based on 3 variables.


I have added 3 UITextFields where the user enters any one of the parameters and then presses a UIButton to calculate.


I would like the answer to return in the same UITextFields used to enter the initial values, is this possible?


So for example:


The user enters a value in box 1 and gets results in box 2 and 3. But then they can also enter a value in box 3 and see results in boxes 1 and 2.


Any help would be much appreciated, thanks.


J

Accepted Reply

Yes it is easy, but will that make real sense to user ?

How do you know which tetField to use ? Is it because all other textFields should be empty ?

What do you enter in box 1, in box 2 ?

What do you want in box 3 ? But then, why replicate in box 2 ?


What you need:

- create IBOutlets for each textFields:

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

When you hit calculate, in the IBAction:


     @IBAction func calculate(_ sender: UIButton) {
              // If you add text1 and text2 for instance:
          let val1 = Int(textField1.text) ?? 0          // If not an Int
          let val2 = Int(textField2.text) ?? 0          // If not an Int
          let result = val1 + val3
          textField3.text = String(result)
          // If you want to copy also in textField2
          textField2.text = String(result)
}

Replies

Yes it is easy, but will that make real sense to user ?

How do you know which tetField to use ? Is it because all other textFields should be empty ?

What do you enter in box 1, in box 2 ?

What do you want in box 3 ? But then, why replicate in box 2 ?


What you need:

- create IBOutlets for each textFields:

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

When you hit calculate, in the IBAction:


     @IBAction func calculate(_ sender: UIButton) {
              // If you add text1 and text2 for instance:
          let val1 = Int(textField1.text) ?? 0          // If not an Int
          let val2 = Int(textField2.text) ?? 0          // If not an Int
          let result = val1 + val3
          textField3.text = String(result)
          // If you want to copy also in textField2
          textField2.text = String(result)
}

Thanks this has helped, I have managed to get further. However, when I enter a value into box 1, it gives me the answer for box 2 (great). But then the value in box 1 changes to "inf". It seems I have created some sort of loop. I have tried getting around it with an if statement but not had much luck. This is the code:


@IBAction func Calculate(_ sender: UIButton) {


let Freq = Float(FrequencyField.text!) ?? 0

let Length = Float(WaveLengthField.text!) ?? 0

let Speed = Float(343.0);

let resultWave = Speed / Freq

let resultFreq = Speed / Length

print(resultFreq)

WaveLengthField.text = String(resultWave)

FrequencyField.text = String(resultFreq)

}

As you do a division, giving a default value of 0 may cause a zero-divide, hence the "inf" value. You should change as follows:


        let freq = Float(frequencyField.text!) ?? 1.0     // If no value, will be set to 1.0
        let length = Float(waveLengthField.text!) ?? 1.0


Don't forget to close the thread if that works OK. And don't hesitate to open new threads if you have further questions.


Note: you should change names of var, func to start with lowerCase, as common practice in Swift.

do it using refactor (right click on a name as Freq) to dio it faster and avoid causing problems with IBOutlets.

Great, thanks!