当我运行我的代码时,它会正确更改 QLabel.text 但不会更改答案文本。所有按钮都保持与选择问题之前相同的文本。它也不会在第一次加载时加载答案。它保留属性检查器中设置的文本。
我正在使用 Xcode 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!")
}
}
}