已编程的 Swift 按钮未显示正确的文本?

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?) {
        
    }
    
}

我创建了一个按钮 class 并设置了该按钮的参数以及它应该如何出现在 screen.Created 这个 levelOne class 的一个对象上 class 并通过一个循环和在顶部创建了一些问题,将答案分配给每个问题的按钮,但它们不显示分配的答案,而是显示预设按钮名称。

一切正常,但选择问题方法无法正常运行,因为它依赖于按下按钮才能进入下一个问题。

需要按钮的解决方案来显示为每个问题分配的答案。

提前谢谢你。

您从未将每个按钮添加到 buttons...

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

我还强烈建议研究基本的 Swift 代码编写实践。像

  1. 驼峰命名惯例
  2. 变量命名而不是 Questions vs Questionss
  3. 初始化为 1 项 - 所以你的结构将是 Question 并且它将包含 title:Stringanswers:[String]answerNumber:[Int] 然后你可以有一个数组 Questions.
  4. 将代码分解成块,即函数。