Function in textFieldDidEndEditing is running when it’s not supposed to run

Advertisements

I’ve got 5 text fields:

When I type values in any 4 of the text fields, the value should be calculated and it should appear in the 5th text field.

For example, if I enter values in the Interest, Monthly Payment, Future Value and Number of Payments text fields, a value should be calculated and it should appear on the Principal Amount text field.
The calculation takes place when the Keyboard is dismissed (textFieldDidEndEditing).

Here is the code for that:

// When the user has finished typing
func textFieldDidEndEditing(_ textField: UITextField) {

    // Principal Amount
    let P = Double(principalAmountTextField.text!)
    // Interest rate
    let r = Double(interestTextField.text!)
    // Monthly Payment
    let PMT = Double(monthlyPaymentTextField.text!)
    // Number of Payments
    let t = Double(numOfPaymentsTextField.text!)
    // Future Value
    let A = Double(futureValueTextField.text!)
    
    

    // Check if the necessary text fields are filled to find Principal Amount
    if futureValueTextField.text != "" && interestTextField.text != "" && numOfPaymentsTextField.text != "" && monthlyPaymentTextField.text != "" {
        print(1)

        principalAmountTextField.text = String(calculatePrincipalAmountCompound(futureValue: A!, interestRate: r! / 100, payment: PMT!, numOfPayments: t!))
    }

    // Check if the necessary text fields are filled to find Monthly Payment
    if futureValueTextField.text != "" && principalAmountTextField.text != "" && numOfPaymentsTextField.text != "" && interestTextField.text != "" {
        print(2)
        
        monthlyPaymentTextField.text = String(calculateMonthlyPaymentCompound(principalAmount: P!, futureValue: A!, numOfPayments: PMT!, interestRate: r! / 100))
    }
}

So clearly, if I enter values in the Interest, Monthly Payment, Future Value and Number of Payments text fields, only the first IF condition in the textFieldDidEndEditing method should run, but for some reason, the second IF condition runs too.

I have no idea why it runs, I’d appreciate it if someone could help me out.

EDIT:
Thanks to @tomerpacific I was able to make this work.
But I still don’t know why the above code doesn’t work, I just tried running the IF conditions without calling the methods and it works properly:

// Check if the necessary text fields are filled to find Principal Amount
if futureValueTextField.text != "" && interestTextField.text != "" && numOfPaymentsTextField.text != "" {
        
    print(11)
}
    
// Check if the necessary text fields are filled to find Monthly Payment
if futureValueTextField.text != "" && principalAmountTextField.text != "" && numOfPaymentsTextField.text != "" && interestTextField.text != "" {

    print(22)
}

>Solution :

The problem lies with your logic.

Let’s break it down to make things clearer.

if futureValueTextField.text != "" && interestTextField.text != "" && numOfPaymentsTextField.text != "" && monthlyPaymentTextField.text != "" {
        print(1)

        principalAmountTextField.text = String(calculatePrincipalAmountCompound(futureValue: A!, interestRate: r! / 100, payment: PMT!, numOfPayments: t!))
    }

When this if condition is true, principalAmountTextField gets a value.

So, after this condition, the following text fields have values:

  • futureValueTextField

  • interestTextField

  • numOfPaymentsTextField

  • monthlyPaymentTextField

  • principalAmountTextField

    // Check if the necessary text fields are filled to find Monthly Payment
      if futureValueTextField.text != "" && principalAmountTextField.text != "" && numOfPaymentsTextField.text != "" && interestTextField.text != "" {
          print(2)
    
          monthlyPaymentTextField.text = String(calculateMonthlyPaymentCompound(principalAmount: P!, futureValue: A!, numOfPayments: PMT!, interestRate: r! / 100))
      }
    

Now, since you don’t return from your if clauses or don’t have an if/else flow, the next if evaluates to true when the following textfields have values:

  • futureValueTextField
  • interestTextField
  • numOfPaymentsTextField
  • principalAmountTextField

And since you have given principalAmountTextField a value, the second if condition is met and the code runs.

Leave a ReplyCancel reply