0

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()
    }

}
  • 1
    Or [How do I shuffle an array in Swift](http://stackoverflow.com/q/24026510/1271826). – Rob May 17 '15 at 22:51
  • BTW, in the future, please post [MCVE](http://stackoverflow.com/help/mcve), i.e. the smallest possible example to reproduce the problem. This question includes a lot of code that was not directly relevant to the question at hand. – Rob May 17 '15 at 22:56
  • how can i use this code on my app? var previousNumber: UInt32? // used in randomNumber() func randomNumber() -> UInt32 { var randomNumber = arc4random_uniform(10) while previousNumber == randomNumber { randomNumber = arc4random_uniform(10) } previousNumber = randomNumber return randomNumber } – Jonathan Jimenez May 17 '15 at 23:00
  • That code just generates a random number as long as the random number is not the same as the prior number. If you want to randomly go through all of your questions, making sure you don't repeat any of them, see [hamobi's answer](http://stackoverflow.com/a/27541537/1271826) (which is, in effect, what chedabob recommended below) or see that post about [shuffling an array](http://stackoverflow.com/q/24026510/1271826). – Rob May 17 '15 at 23:26
  • does anyone know on my code of how can i make it go to a different view controller on the story board after four questions have been asked . – Jonathan Jimenez May 19 '15 at 15:56
  • To programmatically transition to another scene, you create segue between the view controllers and then call `performSegueWithIdentifier`. See http://stackoverflow.com/a/27650207/1271826 – Rob May 19 '15 at 16:17

1 Answers1

2

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.

chedabob
  • 5,835
  • 2
  • 24
  • 44
  • I understand what you say but would it be possible if you can show me an example, i tried something similar before but i fail. – Jonathan Jimenez May 17 '15 at 22:11
  • does anyone know on my code of how can i make it go to a different view controller on the story board after four questions have been asked . – Jonathan Jimenez May 19 '15 at 15:56