UIAlertController: Alert reagiert nicht

Diese Seite verwendet Cookies. Durch die Nutzung unserer Seite erklären Sie sich damit einverstanden, dass wir Cookies setzen. Weitere Informationen

  • UIAlertController: Alert reagiert nicht

    Hallo zusammen,

    ich versuche mich gerade in Swift einzuarbeiten. Hierfür habe ich mir eine kleine QuizApp ausgesucht. Allerdings habe ich mich so heftig verstrickt, sodass ich nicht mehr weiter weiß warum mein Alert nach dem Quiz beendet wurde nicht greift. Dieser sollte dem Nutzer fragen ob er das Quiz wiederholen möchte oder es beenden möchte und somit zu einer anderen View geleitet wird.
    Habt ihr einen Tipp für mich Newbie?

    Vielen Dank!


    Quellcode

    1. import UIKit
    2. class QuestionData2ViewController: UIViewController {
    3. var questiondata2 = QuestionData2()
    4. var quizManager2 = QuizManager()
    5. var feedback = UIImpactFeedbackGenerator(style: .heavy)
    6. var currentQuestionIndex = 0
    7. var highscore2 = 0
    8. // MARK: - Outlets
    9. @IBOutlet weak var questionLabel2: UILabel!
    10. @IBOutlet weak var option1_2: UIButton!
    11. @IBOutlet weak var option2_2: UIButton!
    12. @IBOutlet weak var option3_2: UIButton!
    13. @IBOutlet weak var option4_2: UIButton!
    14. @IBOutlet weak var progressBar2: UIProgressView!
    15. // MARK: - Actions
    16. @IBAction func answerButtonTapped2(_ sender: UIButton) {
    17. if questiondata2.checkAnswer(sender.currentTitle!) {
    18. feedback.impactOccurred()
    19. sender.tintColor = UIColor.green
    20. } else {
    21. feedback.impactOccurred()
    22. sender.tintColor = UIColor.red
    23. }
    24. DispatchQueue.main.asyncAfter(deadline: .now() + 1.0) {
    25. if self.questiondata2.isFinish() {
    26. self.presentQuizEndAlert()
    27. } else{
    28. self.questiondata2.nextQuestion()
    29. print("Current question number: \(self.questiondata2.questionNumber2)")
    30. self.updateUserInterface()
    31. }
    32. }
    33. }
    34. func updateUserInterface() {
    35. questionLabel2.text = questiondata2.ernährungsquiz[questiondata2.questionNumber2].text
    36. option1_2.setTitle(questiondata2.ernährungsquiz[questiondata2.questionNumber2].answer[0], for: .normal)
    37. option2_2.setTitle(questiondata2.ernährungsquiz[questiondata2.questionNumber2].answer[1], for: .normal)
    38. option3_2.setTitle(questiondata2.ernährungsquiz[questiondata2.questionNumber2].answer[2], for: .normal)
    39. option4_2.setTitle(questiondata2.ernährungsquiz[questiondata2.questionNumber2].answer[3], for: .normal)
    40. // Setzt die Farbe des Buttons zurück
    41. option1_2.tintColor = UIColor.orange
    42. option2_2.tintColor = UIColor.orange
    43. option3_2.tintColor = UIColor.orange
    44. option4_2.tintColor = UIColor.orange
    45. progressBar2.progress = questiondata2.getProgress()
    46. }
    47. func presentQuizEndAlert() {
    48. let alert = UIAlertController(title: "Glückwunsch, du hast das Quiz beendet!", message: "Möchtest du das Quiz wiederholen?", preferredStyle: .alert)
    49. let repeatAction = UIAlertAction(title: " JA!!!", style: .default) { (alertAction) in
    50. self.quizManager2.restartQuiz()
    51. self.currentQuestionIndex = 0
    52. self.updateUserInterface()
    53. }
    54. let cancelAction = UIAlertAction(title: "NEIN", style: .cancel) { (alertAction) in
    55. self.performSegue(withIdentifier: "HomeViewController", sender: nil)
    56. }
    57. alert.addAction(repeatAction)
    58. alert.addAction(cancelAction)
    59. DispatchQueue.main.async {
    60. self.present(alert, animated: true, completion: nil)
    61. }
    62. }
    63. override func viewDidLoad() {
    64. super.viewDidLoad()
    65. updateUserInterface()
    66. }
    67. }
    Alles anzeigen