Pass one integer value to multiple view controllers

Hello everyone,

 

I am new to this forum, and new to Xcode/Swift in general. I’ve done a bit of research, but didn’t find any similar topic. Sorry if the question has been asked already

 

So, to make it simple, I am trying to code a quizz app. I have 3 VCs so far :

– ViewController.swift, with a button that redirects to my 1st Quiz

– Quiz1ViewController.swift where 4 questions are asked, and the score is saved into a variable counts “points”

– EndQuiz1ViewController.swift, where the score of my Quiz1 is displayed

 

I am passing “points” to the endQuiz1VC just fine thanks to the following piece of code in Quiz1VC :

 


        let destVC = segue.destination as! EndQuiz1ViewController
        destVC.intPassed = points

 

However, the app totally freaks out when I use the same piece of code to pass my “points” value into MainVC. What other formula do you recommend me to use ?

 

See full code below for more info.

 

Thanks in advance,

Mathilde

 

—- full code —-

 


/
/
/
/
/
/
/ 
import UIKit
class Quiz1ViewController: UIViewController {
   
    let questions = ["question 1", "question 2", "question 3", "question 4"]
    let answers = [["Bonne réponse", "Mauvaise Réponse", "Mauvaise Réponse", "Mauvaise Réponse"], ["Bonne réponse", "Mauvaise Réponse", "Mauvaise Réponse", "Mauvaise Réponse"], ["Bonne réponse", "Mauvaise Réponse", "Mauvaise Réponse", "Mauvaise Réponse"], ["Bonne réponse", "Mauvaise Réponse", "Mauvaise Réponse", "Mauvaise Réponse"]]
    /
    var currentQuestion = 0
    var rightAnswerPlacement:UInt32 = 0
    var points = Int()
   
    /
    @IBOutlet weak var lbl: UILabel!
   
    /
    @IBAction func action(_ sender: AnyObject)
    {
        if (sender.tag == Int(rightAnswerPlacement))
        {
            print("Right")
            points += 1
        }
        else
        {
            print("Wrong")
        }
        if (currentQuestion != questions.count)
        {
            newQuestion()
        }
        else
        {
            end()
        }

    }
   
    override func viewDidAppear(_ animated: Bool)
    {
        newQuestion()
       
    }
   
   
    /
    func end()
    {
        print(points)
        performSegue(withIdentifier: "showEndQuiz1", sender: self)
    }
   
    override func prepare(for segue:UIStoryboardSegue, sender: Any?)
    {
        let destVC = segue.destination as! EndQuiz1ViewController
        destVC.intPassed = points
          /* let VC = segue.destination as! ViewController
        VC.ScoreQuiz = points */
    }
    /
    func newQuestion()
    {
        lbl.text = questions[currentQuestion]
        rightAnswerPlacement = arc4random_uniform(4)+1
       
        /
        var button:UIButton = UIButton()
        var x = 1
        for i in 1...4
        {
            /
            button = view.viewWithTag as! UIButton
           
            if (i == Int(rightAnswerPlacement))
            {
                button.setTitle(answers[currentQuestion][0], for: .normal)
            }
            else
            {
                button.setTitle(answers[currentQuestion][x], for: .normal)
                x = 2
            }
        }
        currentQuestion += 1
    }
   
    override func viewDidLoad() {
        super.viewDidLoad()
        /
    }
    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        /
    }
   
    /
    / 
    /
    override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
        /
        /
    }
    */
}

Powered by WPeMatico

About

You may also like...

Comments are closed.