keyboard will not dismiss

The keyboard is not a problem, but will not dismiss when the app is loaded onto a real phone.

In line 21 below we tried resignFirstResponder(), but it did not work.

We are unsure on where to put the line to resignFirstResponder().

Any help you can provide is appreciated.


    @IBOutlet weak var textField: UITextField!
    @IBOutlet weak var countryImage: UIImageView!
    @IBOutlet weak var correctionLabel: UILabel!
    @IBOutlet weak var scoreLabel: UILabel!
    
    @IBOutlet weak var countryLinks: UIButton!
    @IBAction func countryLinksPressed(_ sender: UIButton) {
       if let url = URL(string: "\(questions[questionNumber].countryLink)") {
            UIApplication.shared.open(url, options: [:])
        }
    }
    
    var questionNumber = 0
    var score = 0
    var response = ""
    
    @IBOutlet weak var submitButton: UIButton!
    @IBAction func submitButtonPressed(_ sender: Any) {
        response = textField.text!
        //line below causes a crash
        //textField.resignFirstResponder()
        if (response.lowercased() == questions[questionNumber].correctAnswer) {
        score += 1
        scoreLabel.text = "Score: \(score)"
        correctionLabel.text = "Correct"
        correctionLabel.textColor = UIColor.green
            }
        
    else {
        correctionLabel.text = "Incorrect"
        correctionLabel.textColor = UIColor.red
            }
            
     if (questionNumber + 1 == questions.count) {
        endQuiz()
        }
    }

Accepted Reply

Line 21 should work.

Does it crash ? What error ?

It is surprising if line 19 does not crash.


Are you sure crash is on line 21 ? Not on 22 ?

Are you sure you have connected properly textField IBOutlet to its object in Interface Builder ?

What do you do on endQuiz() ? Do you leave the app ?

You do not show how you increment questionNumber

Replies

Line 21 should work.

Does it crash ? What error ?

It is surprising if line 19 does not crash.


Are you sure crash is on line 21 ? Not on 22 ?

Are you sure you have connected properly textField IBOutlet to its object in Interface Builder ?

What do you do on endQuiz() ? Do you leave the app ?

You do not show how you increment questionNumber

You were right. The problem was not line 21, it was line 19.


Thank you so much for your help.