textFieldShouldReturn Not Checking My Answers

I use a func textFieldShouldReturn that depending on what Integer I have in the textField it will either display an x or a check. I have 3 textFields but the func only works for numAnswerOne. How do I make it work for all 3 textFields?

Code Block
func textFieldShouldReturn(_ textField: UITextField) -> Bool {
    //makes a return key work
    textField.resignFirstResponder()
    let text = textField.text
    let numAnswerOne = Int(answerOne.text!)
    let numAnswerTwo = Int(answerTwo.text!)
    let numAnswerThree = Double(answerThree.text!)
     
    if numAnswerOne == 70 {
      checkOne.alpha = 1
      wrongOne.alpha = 0
      answerOne.isUserInteractionEnabled = false
    } else if numAnswerOne != 70 {
      wrongOne.alpha = 1   //function stops here
    } else if numAnswerTwo == 2 {
      checkTwo.alpha = 1
      wrongTwo.alpha = 0
      answerTwo.isUserInteractionEnabled = false
    } else if numAnswerTwo != 2 {
      wrongTwo.alpha = 1
    } else if numAnswerThree == 30.1 {
      checkThree.alpha = 1
      checkThree.alpha = 0
      checkThree.isUserInteractionEnabled = false
    } else if numAnswerThree != 30.1 {
      checkThree.alpha = 0
    }
    return true
  }

Answered by Claude31 in 649419022
Please show what your code is exactly now.

Are you sure you have defined the tags in Interface Builder as 1, 2, 3, to match the code ?
May be you just forgot to set it for numAnswerThree.

To understand what's going on, add a print after each test, such as:
Code Block
if textField.tag == 1 {
print("numAnswerOne")
// continue


If that works, don't forget to mark the correct answer.
If not, please report with the print results.

 it will either display an x or a check

Where is it in code ?

In the textFieldShouldReturn func, you should test which textField it is and act accordingly.

The problem is that the first 2 tests cover everything:
Code Block
if numAnswerOne == 70 {
checkOne.alpha = 1
wrongOne.alpha = 0
answerOne.isUserInteractionEnabled = false
} else if numAnswerOne != 70 {
wrongOne.alpha = 1 //function stops here

Whatever the value of numberOne, it will be covered by these 2, so function stops here.

Change with:
Code Block
if textField == numAnswerOne {
if numAnswerOne == 70 {
checkOne.alpha = 1
wrongOne.alpha = 0
answerOne.isUserInteractionEnabled = false
} else if numAnswerOne != 70 {
wrongOne.alpha = 1 //function stops here
}
}
if textField == numAnswerTwo {
if numAnswerTwo == 2 {
checkTwo.alpha = 1
wrongTwo.alpha = 0
answerTwo.isUserInteractionEnabled = false
} else if numAnswerTwo != 2 {
wrongTwo.alpha = 1
}
}
if textField == numAnswerThree {
if numAnswerThree == 30.1 {
checkThree.alpha = 1
checkThree.alpha = 0
checkThree.isUserInteractionEnabled = false
} else if numAnswerThree != 30.1 {
checkThree.alpha = 0
}
}

I have to convert it my textFields to NSObjects and it does not work in the end. The check marks are the check followed by a number and the xs are wrong followed by a number.
I have to convert it my textFields to NSObjects and it does not work in the end. The check marks are the check followed by a number and the xs are wrong followed by a number.

Sorry, what you explain is incomprehensible.

You started by saying that it was only working with the first textField.

With the change I proposed, does it work for all ?
If you have a problem with
Code Block
if textField == numAnswerOne


then just set the tags of each textField to 1, 2 and 3
then test
Code Block
if textField.tag == 1

Alright it seems to work but when I get to get the the third textfield it seems the code seems to stop and it does not show anything.

Accepted Answer
Please show what your code is exactly now.

Are you sure you have defined the tags in Interface Builder as 1, 2, 3, to match the code ?
May be you just forgot to set it for numAnswerThree.

To understand what's going on, add a print after each test, such as:
Code Block
if textField.tag == 1 {
print("numAnswerOne")
// continue


If that works, don't forget to mark the correct answer.
If not, please report with the print results.
Alright I was able to fix it with your suggestion.
textFieldShouldReturn Not Checking My Answers
 
 
Q