Search code examples
swiftxcodebuttonsprite-kitskspritenode

Programmed Swift button not displaying correct text?


import UIKit
import SpriteKit
import GameplayKit

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

class LevelOne: SKScene {
    
    var button = Button()
    
    var buttons = [Button]()
    let Question = UILabel(frame: CGRect(x: 0.3, y: 0.3, width: 40, height: 21))
    
    var Questionss = [Questions]()
    
    var QNumber = Int()
    
    var buttonNames = [""]
    
    override func didMove(to view: SKView) {
        // Button code
        let stacView = UIStackView()
        stacView.spacing = 12
        stacView.distribution = .fillEqually
        stacView.axis = .horizontal
        stacView.translatesAutoresizingMaskIntoConstraints = false
        view.addSubview(stacView)
        
        buttonNames = ["One","Two","Three","Four"]
        
        for name in buttonNames {
            button = Button()
            button.setTitle(name, for: .normal)
            stacView.addArrangedSubview(button)
        }
        
        NSLayoutConstraint.activate([stacView.centerXAnchor.constraint(equalTo: view.centerXAnchor),stacView.centerYAnchor.constraint(equalTo: view.centerYAnchor),stacView.widthAnchor.constraint(equalToConstant: 350),stacView.heightAnchor.constraint(equalToConstant:70)])
        
        // Question and Answer Code
        Questionss = [Questions(Question: "What is a Car ?", Answers: ["Bat","Cat","Vehicle","Pat"], Answer: 3),
                      
        Questions(Question: "What is a plane ?", Answers: ["Bat","Aircraft","Cat","Pat"], Answer: 2),

        Questions(Question: "What is a ant ?", Answers: ["Bat","Cat","Vegetable","Insect"], Answer: 4),

        Questions(Question: "What is a Apple ?", Answers: ["Bat","Cat","Fruit","Pat"], Answer: 3),]
        
        Question.frame = CGRect(x: 330, y: 70, width: 200, height: 21)
        Question.backgroundColor = UIColor.orange
        Question.textColor = UIColor.white
        Question.textAlignment = NSTextAlignment.center
        Question.text = "What caused Global Warming ?"
        self.view?.addSubview(Question)
        
        let background = SKSpriteNode(imageNamed: "background")
        background.position = CGPoint(x: self.size.width/2, y: self.size.height/2)
        background.zPosition = 0
        self.addChild(background)
        
        PickQuestion()
        
    }
    
    @objc func PickQuestion(){
        if Questionss.count > 0{
            QNumber = 0
            Question.text = Questionss[QNumber].Question
            
            for i in 0..<buttons.count{
                buttons[i].setTitle(Questionss[QNumber].Answers[i], for: UIControl.State.normal)
            }
            Questionss.remove(at: QNumber)
            
        } else {
            print("Done!")
        }
    }
    
    @objc func handleButton() {
        
    }
    
    override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
        
    }
    
}

I created a button class and set the parameters for the button and how it should appear on screen.Created a object in this levelOne class and programmed four buttons through a loop and on top created some question which assign the answers onto the buttons for each question but they do not display the assigned answers instead display the preset button names.

Everything works but pick question method won't function properly as it relies the button to be pressed in order to move onto the next question.

Need solution for the button to display the answers assigned for each question.

Thank you in advance.


Solution

  • You never added each button to buttons...

    for name in buttonNames {
        button = Button()
        button.setTitle(name, for: .normal)
        stacView.addArrangedSubview(button)
        buttons.append(button)
    }
    

    I would also HIGHLY recommend looking into basic Swift code writing practices. Something like

    1. camelCase naming practices
    2. variable naming instead of Questions vs Questionss
    3. Initialization as 1 item - so your struct would be Question and it would contain title:String, answers:[String] and answerNumber:[Int] then you could have an array of Questions.
    4. Breaking up code into chunks, i.e. functions.