Function not using my entire array to change on screen button text

45 views Asked by At

When i Run my code, it correctly changes the QLabel.text but it won't change the answers text. all of the buttons keep the same text as it were before picking a questions. Nor does it load the answers on first load. it keeps the text set in the attributes inspector.

I'm using Xcode Version 3.0

import UIKit



struct Question
{
    var Question : String!
    var Answers : [String]!
    var Answer : Int!
}


class AnimalViewController: UIViewController {



    @IBOutlet weak var QLabel: UILabel!
    @IBOutlet var Buttons: [UIButton]!

   var Questions = [Question]()

    var QNumber = Int()

    var AnswerNumber = Int()



    override func viewDidLoad() {
        super.viewDidLoad()




        Questions = [Question(Question: "What is the fastest fish in the ocean?", Answers: ["Flounder","Sailfis","Swordfish","Lionfish","Tiger Shark"], Answer: 1),
                     Question(Question: "Which animal has the most legs?", Answers: ["Shrimp","Octopus","Millipede","Dog","Lion"], Answer: 2),
                     Question(Question: "What are baby beavers called?", Answers: ["Pups","Joeys","Beaves","Kids","Kittens or Kits"], Answer: 4)]

        PickQuestion()
    }


    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }


    func PickQuestion(){

        if Questions.count > 0
        {
            QNumber = 0
            QLabel.text = Questions[QNumber].Question

            AnswerNumber = Questions[QNumber].Answer

            for i in 0..<Buttons.count
                {
                Buttons[i].setTitle(Questions[QNumber].Answers[i], for: UIControlState.normal)
                }
            Questions.remove(at: QNumber)
        }
        else
        {
            NSLog("Done")
        }

    }

    @IBAction func Btn1(_ sender: AnyObject) {

        if AnswerNumber == 0
        {
            PickQuestion()
        }
        else
        {
            NSLog("Wrong!")
        }

    }

    @IBAction func Btn2(_ sender: AnyObject) {

        if AnswerNumber == 1
        {
            PickQuestion()
        }
        else
        {
            NSLog("Wrong!")
        }
    }

    @IBAction func Btn3(_ sender: AnyObject) {

        if AnswerNumber == 2
        {
            PickQuestion()
        }
        else
        {
            NSLog("Wrong!")
        }
    }

    @IBAction func Btn4(_ sender: AnyObject) {

        if AnswerNumber == 3
        {
            PickQuestion()
        }
        else
        {
            NSLog("Wrong!")
        }
    }

    @IBAction func Btn5(_ sender: AnyObject) {

        if AnswerNumber == 4
        {
            PickQuestion()
        }
        else
        {
            NSLog("Wrong!")
        }
    }


}
0

There are 0 answers