Search code examples
iosswiftuiviewcontrollersegueuistoryboardsegue

How to transfer to a specific storyboard if a condition is true?


I'm developing a simple quizzes app.

I have a storyboard with textview(contains a question)and 3 buttons (answers) when the user presses an answer I need to check if the answer correct or not. if it is correct another storyboard (contain a successful message) will be present. so I made an action for each button but the problem is that the second storyboard(successful message ) appears only a few seconds and then the previews storyboard(question ) appears again .. I want the successful message to be in the screen here is the code:

  @IBAction func click1(_ sender: Any) {
    if (opt1.titleLabel?.text) != nil {
        let userAnswer=opt1.titleLabel?.text;

        if ((userAnswer!.elementsEqual(self.optionsArray[0])) == true)
        {
           playSound(fileName: "kidsCheering")

            let storyBoard: UIStoryboard = UIStoryboard(name: "Main", bundle: nil)
            let QMessagesViewController = storyBoard.instantiateViewController(withIdentifier: "QMessage") as! QMessagesViewController
            self.present(QMessagesViewController, animated: true, completion: nil)
        }

Thank you

and here is the error that i got : Attempt to present on while a presentation is in progress!


Solution

  • Please change your viewcontroller variable name like this and remove segue if added or use only use segue:

        let qMessagesVC = storyBoard.instantiateViewController(withIdentifier: "QMessage") as! QMessagesViewController
        self.present(qMessagesVC, animated: true, completion: nil)