UIAlertAction Handler is not being called

97 views Asked by At

I have a UIAlertController that displays after a user incorrectly answers a question. I added a UIAlertAction on the alert that allows you to retry the level. I want it to take the user back to the home page. However, whenever I click on the Retry action, it gets dismissed and nothing happens.

Here is the code:

func showAlert(message: String) {
    let alert = UIAlertController(title: "You lost!", message: message, preferredStyle: .alert)
    alert.addAction(UIAlertAction(title: "Retry", style: .default) { action in
        self.performSegue(withIdentifier: "goToHomePage", sender: nil)
    })
    self.present(alert, animated: true, completion: nil)
}

I am calling this function in my noteCorrect function which will display the alert if the user incorrectly answers the question.

This is the noteCorrect function:


func noteCorrect(with note: String, accidental: Int, firstIndex: Int) {
    
    let selectedRow = accidentalPicker.selectedRow(inComponent: 0)
    
    self.rect.removeFromSuperview()
    
    guard let text = noteTextField.text else { return }
    
    if text == note && selectedRow == accidental {
        correctLabel.text = "Correct!"
        correctLabel.textColor = .green
        width -= 20
        rect.frame = CGRect(x: self.view.frame.maxX / 2 - 100, y: self.view.frame.midY + 100, width: width, height: height)
        rect.backgroundColor = .red
        self.view.addSubview(rect)
    } else {
        correctLabel.text = "Incorrect!"
        correctLabel.textColor = .red
        correctAnswer = "Sorry, the correct answer is " + note + "\(pickerData[accidental])"
        showAlert(message: correctAnswer)
        
        let layer = noteBossImage.layer
        stopAnimation(layer: layer)
        
        rect.frame = CGRect(x: self.view.frame.maxX / 2 - 100, y: self.view.frame.midY + 100, width: width, height: height)
        rect.backgroundColor = .red
        self.view.addSubview(rect)
    }
}

Does anyone know why the handler is not being called?

0

There are 0 answers