Search code examples
iosswiftuiviewcontrollerseguedismiss

Getting : "Attempt to dismiss from view controller while a presentation or dismiss is in progress"


  1. A Swift 4 quiz app.
  2. First view - select num of questions to solve. (use segue to second view)
  3. Second view - start quiz ->checked answer and send "correct" or "wrong" msg to third view. (use segue way to third view)

When the third view appears I get the error:

Attempt to dismiss from view controller <XXX.StartQuizViewController: 0x7fc893f09770> while a presentation or dismiss is in progress!

I've tried using:

DispatchQueue.main.asyncAfter(deadline: .now() + 1.0, execute:....

The warning message goes away but the problem is that I have no access to all the variables that I obtained from the second view for use in the third view.

override func viewDidLoad() {
    super.viewDidLoad()

    DispatchQueue.main.asyncAfter(deadline: .now() + 1.0, execute: {
        if self.answerStatus == "Correct!" {
            self.correctWrongStatusLabel.text = self.answerStatus
            self.praiseLabel.text = "Good job!"
            self.correctAnswerIsLabel.text = ""
            let timer = Timer.scheduledTimer(timeInterval: 1.0, target: self, selector: #selector(self.fireTimer), userInfo: nil, repeats: false)
            self.fireTimer()

        } else if self.answerStatus == "Wrong!" {
            self.correctWrongStatusLabel.text = self.answerStatus
            self.praiseLabel.text = "Maybe next time!"
            self.correctAnswerIsLabel.text = "回答 : \(self.correctAnswerIs)"
            let timer = Timer.scheduledTimer(timeInterval: 3.0, target: self, selector: #selector(self.fireTimer), userInfo: nil, repeats: false)
            self.fireTimer()
        }
    })

@objc func fireTimer() {
    delegate?.answerStatusReceived(answerStatusString: answerStatusString)
    self.dismiss(animated: true, completion: nil)
}

After the second view appear with the quiz, when the user select an answer, it will checked it against the correct answer and display a response saying the answer is correct or wrong. the response view must automatically closes after displaying for XX seconds.


Solution

  • simply use:-- dismiss(animated: true, completion: nil) and omit self