Search code examples
arraysswiftarc4random

how can i make it go to a different view controller on the story board after four questions have been asked?


im trying to either make the NExt button disappear after four question have answer and show a different button that takes to a different view controller where i can add button like main menu or the score or something like that. thanks

import UIKit

class ViewController: UIViewController {

    @IBOutlet weak var QuestionImage: UIImageView!

    @IBOutlet var button1: UIButton!

    @IBOutlet var button2: UIButton!

    @IBOutlet var button3: UIButton!

    @IBOutlet var button4: UIButton!

    @IBOutlet var labelEnd: UILabel!

    @IBOutlet var NExt: UIButton!

    var CorrectAnswer = String()

    override func viewDidLoad() {
        super.viewDidLoad()
        hide()
        randomQuestion()
    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
    }

    func randomQuestion(){

        var randomNumber:Int = (Int)(arc4random() % 3 + 1)
    while find(askedquestions,randomNumber) != nil && askedquestions.count < 4 {
        randomNumber = (Int)(arc4random() % 3 + 1)
    }
    if askedquestions.count > 4 {
        return
    }

    askedquestions.append(randomNumber)

        switch(randomNumber){

        case 1:
            self.QuestionImage.image = UIImage(named: "apple")

            button1.setTitle("potato", forState: UIControlState.Normal)
            button2.setTitle("carrot", forState: UIControlState.Normal)
            button3.setTitle("apple", forState: UIControlState.Normal)
            button4.setTitle("table", forState: UIControlState.Normal)
            CorrectAnswer = "3"
            break

        case 2:
            self.QuestionImage.image = UIImage(named: "table")

            button1.setTitle("table", forState: UIControlState.Normal)
            button2.setTitle("apple", forState: UIControlState.Normal)
            button3.setTitle("mac", forState: UIControlState.Normal)
            button4.setTitle("windows", forState: UIControlState.Normal)
            CorrectAnswer = "1"
            break

        case 3:
            self.QuestionImage.image = UIImage(named: "windows")

            button1.setTitle("apple", forState: UIControlState.Normal)
            button2.setTitle("car", forState: UIControlState.Normal)
            button3.setTitle("potato", forState: UIControlState.Normal)
            button4.setTitle("windows", forState: UIControlState.Normal)
            CorrectAnswer = "4"
            break

        case 4:
            self.QuestionImage.image = UIImage(named: "mac")

            button1.setTitle("windows", forState: UIControlState.Normal)
            button2.setTitle("car", forState: UIControlState.Normal)
            button3.setTitle("mac", forState: UIControlState.Normal)
            button4.setTitle("sky", forState: UIControlState.Normal)
            CorrectAnswer = "3"
            break

        default:
            break
        }
    }


    func hide(){
        labelEnd.hidden = true
        NExt.hidden = true
    }
    func unHide(){
        labelEnd.hidden = false
        NExt.hidden = false
    }

    @IBAction func button1Action(sender: AnyObject) {
        unHide()
        if (CorrectAnswer  == "1"){
            labelEnd.text = "Correct"

        } else {
            labelEnd.text = "wrong"
        }
    }

    @IBAction func button2Action(sender: AnyObject) {
        unHide()
        if (CorrectAnswer  == "2"){
            labelEnd.text = "Correct"
        } else {
            labelEnd.text = "wrong"
        }
    }


    @IBAction func button3Action(sender: AnyObject) {
        unHide()
        if (CorrectAnswer  == "3"){
            labelEnd.text = "Correct"
        } else {
            labelEnd.text = "wrong"
        }
    }


    @IBAction func button4Action(sender: AnyObject) {
        unHide()
        if (CorrectAnswer  == "4"){
            labelEnd.text = "Correct"
        } else {
            labelEnd.text = "wrong"
        }
    }

    @IBAction func next(sender: AnyObject) {

        randomQuestion()
        hide()
    }

}

Solution

  • I'd start by encapsulating all of your data into a Question object for each question.

    Then put all of your questions in an array, and each time you get a random item out of the array, remove it. Repeat until the array is empty and the game is over.