0

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!")
        }
    }


}
Chris Levely
  • 131
  • 1
  • 10
  • You never add anything to `Buttons`, so `Buttons.count` is 0 and the loop never runs. – Wyetro Oct 19 '16 at 00:45
  • how would I go about adding whats in my Questions array answers to my buttons? very beginner question I know – Chris Levely Oct 19 '16 at 00:56
  • You need IBOutlets for each Button and then add those to Buttons in viewDidLoad. – Wyetro Oct 19 '16 at 01:00
  • https://www.youtube.com/watch?v=dyxqsfrCaeM I used this tutorial by Jared Davidson, and he never set those up that way. so that is why i am confused. I don't know the difference between mine and his – Chris Levely Oct 19 '16 at 01:02
  • Then I'd make sure that the IBOutlet for Buttons is wired correctly to all the buttons in your storyboard – Wyetro Oct 19 '16 at 01:07
  • They appear to be. They are correctly referenced by the IBActions and they are all included and referenced by the IBOutlet collection – Chris Levely Oct 19 '16 at 01:17

0 Answers0