Quiz App Pop Up Window does not show up at first tap

Hello CCC: I am working on the Quiz App, for some reason when I start the app and then tap the screen to answer a question the pop up does not appear at the first tap. I need to tap it twice - I have checked everything and I can’t figure out why is that. Please help me. Michael

1 Like

Your code in didSelectRowAt to present the ResultViewController should be:

present(self.resultDialog!, animated: true, completion: nil)

I don’t recall any part of the tutorial specifying that the completion handler had any code and in fact my version of the code is as above.

At what point are you in the Tutorial video series?

Hey Chris, thanks for the fast reply - I put it in (func presentVC () {print"presentedVC")} just to see what the app does and when it calls the completion - after viewDidAppear method as I found out. I was going to start the Networking-Video today but I have been struggling with this issue for 5h…

Thanks - I tried your code but I remember Chris saying:" I think we might don’t need “self”. Anyways I doesn’t work…

self is only needed when you refer to a property within a closure. That might not be a helpful answer as to why self is not needed particularly if the concept of a closure is new, but in time you will understand what that is all about.

Can you post the entire contents of your ViewController code so that we can take a look and identify anything that might be the problem.

The easiest way to do that is to place 3 back-ticks on a new line and then paste the code from ViewController. On the next line after place 3 back-ticks to complete the code insertion block.

Like this:

Which will result in your code looking like this in the reply

import Foundation

class Card {
    
    var imageName = ""
    var isFlipped = false
    var isMatched = false
    
}
import UIKit

class ViewController: UIViewController, QuizProtocol, UITableViewDelegate, UITableViewDataSource, ResultViewControllerProtocol {
    
    @IBOutlet weak var questionLabel: UILabel!
    
    @IBOutlet weak var tableView: UITableView!
    
    var model = QuizModel()
    var questions = [Question]()
    var currentQuestionIndex = 0
    var numCorrect = 0
    
    var resultDialog:ResultViewController?
    
    override func viewDidLoad() {
        super.viewDidLoad()
        
        // Initialize the result dialog
        resultDialog = storyboard?.instantiateViewController(identifier: "ResultVC") as? ResultViewController
        resultDialog?.modalPresentationStyle = .overCurrentContext
        resultDialog?.delegate = self
        
        // Set self as the delegate and datasource for the tableview
        tableView.delegate = self
        tableView.dataSource = self
        
        // Dynamic row heights
        tableView.estimatedRowHeight = 100
        tableView.rowHeight = UITableView.automaticDimension
        
        // Set up the model
        model.delegate = self
        model.getQuestions()
    }
    
    func displayQuestion() {
        
        // Check if there are questions and check that the currentQuestionIndex is not out of bounds
        guard questions.count > 0 && currentQuestionIndex < questions.count else {
            return
        }
        
        // Display the question text
        questionLabel.text = questions[currentQuestionIndex].question
        
        // Reload the answers table
        tableView.reloadData()
    }

    // MARK: - QuizProtocol Methods
    
    func questionsRetrieved(_ questions: [Question]) {
        
        // Get a reference to the questions
        self.questions = questions
        
        // Check if we should restore the state, before showing question #1
        let savedIndex = StateManager.retrieveValue(key: StateManager.questionIndexKey) as? Int
        
        if savedIndex != nil && savedIndex! < self.questions.count {
            
            // Set the current question to the saved index
            currentQuestionIndex = savedIndex!
            
            // Retrieve the number correct from storage
            let savedNumCorrect = StateManager.retrieveValue(key: StateManager.numCorrectKey) as? Int
            
            if savedNumCorrect != nil {
                numCorrect = savedNumCorrect!
            }
        }
        
        // Display the first question
        displayQuestion()
    }

    // MARK: UITableView Delegate Methods
    
    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        
        // Make sure that the questions array actually contains at least a question
        
        guard questions.count > 0 else {
            return 0
        }
        
        // Return the number of answers for this question
        
        let currentQuestion = questions[currentQuestionIndex]
        
        if currentQuestion.answers != nil {
            return currentQuestion.answers!.count
        }
        else {
            return 0
        }
    }
    
    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        
        // Get a cell
        
        let cell = tableView.dequeueReusableCell(withIdentifier: "AnswerCell", for: indexPath)
        
        // Customize it
        
        let label = cell.viewWithTag(1) as? UILabel
        
        if label != nil {
            
            let question = questions[currentQuestionIndex]
            
            if question.answers != nil &&
                indexPath.row < question.answers!.count {
                // Set the answer text for the label
                
                label!.text = question.answers![indexPath.row]
            }
        }
        
        // Return the cell
        return cell
    }
    
    func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
        
        var titleText = ""
        
        // User has tapped on a row, check if it's the right answer
        
        let question = questions[currentQuestionIndex]
        
        if question.correctAnswerIndex! == indexPath.row {
            
            // User got it right
            
            print("User got it right")
            
            titleText = "Correct!"
            numCorrect += 1
        }
        else {
            // User got it wrong
            
            print("User got it wrong")
            
            titleText = "Wrong!"
        }
        
        // Show the popup
        
        if resultDialog != nil {
            
            // Customize the dialog text
            
            resultDialog!.titleText = titleText
            
            resultDialog!.feedbackText = question.feedback!
            
            resultDialog!.buttonText = "Next"
            
            present(resultDialog!, animated: true, completion: nil)
        }
    }
    
    // MARK:  ResultViewControllerProtocol Methods
    
    func dialogDismissed() {
        
        // Increment the currentQuestionIndex
        
        currentQuestionIndex += 1
        
        if currentQuestionIndex == questions.count {
            
            // The user has just answered the last question
            

            // Show a summary dialog
            if resultDialog != nil {
                
                // Customize the dialog text
                resultDialog!.titleText = "Summary"
                resultDialog!.feedbackText = "You got \(numCorrect) correct out of \(questions.count) questions"
                resultDialog!.buttonText = "Restart"
                
                present(resultDialog!, animated: true, completion: nil)
                
                // Clear state
                StateManager.clearState()
            }
        }
        else if currentQuestionIndex > questions.count {
            // Restart
            numCorrect = 0
            currentQuestionIndex = 0
            displayQuestion()
        }
        else if currentQuestionIndex < questions.count {
            // We have more questions to show
            
            // Display the next question
            displayQuestion()
            
            // Save state
            StateManager.saveState(numCorrect: numCorrect, questionIndex: currentQuestionIndex)
        }
        
    }
    
}
1 Like

Here you go Chris thanks for helping out.

hi, i have the same problem!! but this only happens when i began lesson 8 and customising the pop up windows and placing the codes in diff places than b4 like how chris does… but then my pop window just didnt appear anymore, however when i click on the right answer, debugger shows “user got it right/wrong”. i followed all codes but hmm to no avail :confused:

@Michael_Karbasch

Hey Michael,

Sorry it’s been a few days since my last post. I’ve scanned through your code and can’t find an issue there so the problem is possibly elsewhere. Have you solved it in the meantime anyway?

Hi
I have the same issue with this. As naazh22 says, first tap on an answer triggers the appropriate console output, but a second tap is required to trigger the popup - interestingly, the second click can be anywhere on the app view. Subsequent questions & answers work as expected. This is only an issue on first run. I’d be grateful for a suggestion of where to look for a solution.

Hey Chris, thank you. I have just come back from my holidays in Slovenia and have not coded since then. I will let you know shortly. Cheers