gpt4 book ai didi

Swift Quiz - 重置按钮

转载 作者:行者123 更新时间:2023-11-28 13:06:29 27 4
gpt4 key购买 nike

我遵循了一个测验教程并且一切正常,但是当我想添加重置按钮以便测验可以重新开始时它不起作用。这是代码,我试图重置数字,我尝试清空数组但它不起作用。请帮忙。

import UIKit

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

class ViewController: UIViewController {

@IBOutlet var buttons: [UIButton]!
@IBOutlet weak var qLabel: UILabel!
@IBOutlet weak var novceki: UILabel!

var Questions = [Question]()

var Qnumber = Int()

var AnswerNumber = Int()

override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.


Questions = [Question(Question: "Jesi pojeo?" , Answers: ["Jesam", "Nisam", "Budem", "Fuj"], Answer: 0),
Question(Question: "Najvisi vrh piramide?" , Answers: ["10", "39", "40", "55"], Answer: 2),
Question(Question: "Jesi popusio?" , Answers: ["Jesam", "Nisam", "Budem", "Fuj"], Answer: 0),
Question(Question: "Jesi bio vani?" , Answers: ["Jesam", "Nisam", "Budem", "Fuj"], Answer: 3)]

pickQuestion()

}

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

func pickQuestion (){

if Questions.count > 0{
//Qnumber = 0
Qnumber = random() % Questions.count
qLabel.text = Questions[Qnumber].Question

AnswerNumber = Questions[Qnumber].Answer

for i in 0..<buttons.count{
buttons[i].setTitle(Questions[Qnumber].Answers[i], forState: UIControlState.Normal)
}
Questions.removeAtIndex(Qnumber)
}
else {
NSLog("Done!")
}
}


@IBAction func Btn1(sender: UIButton) {

if AnswerNumber == 0 {
pickQuestion()
}
else{

NSLog("CORRECT!")
}
}

@IBAction func Btn2(sender: UIButton) {
if AnswerNumber == 1 {
pickQuestion()
}
else{
NSLog("Wrong!")
}
}

@IBAction func Btn3(sender: UIButton) {
if AnswerNumber == 2 {
pickQuestion()
}
else{
NSLog("Wrong!")
}
}

@IBAction func Btn4(sender: UIButton) {
if AnswerNumber == 3 {
pickQuestion()
}
else{
NSLog("Wrong!")
}
}
@IBAction func reset(sender: UIButton) {
pickQuestion() //- here is the problem, what to put here? it only continue with quiz it does not restart it, tried with reset numbers and array, but don't work
}
}

最佳答案

创建一个函数来重新生成您的问题:

func freshQuestions() -> [Questions] {
return [Question(Question: "Jesi pojeo?" , Answers: ["Jesam", "Nisam", "Budem", "Fuj"], Answer: 0),
Question(Question: "Najvisi vrh piramide?" , Answers: ["10", "39", "40", "55"], Answer: 2),
Question(Question: "Jesi popusio?" , Answers: ["Jesam", "Nisam", "Budem", "Fuj"], Answer: 0),
Question(Question: "Jesi bio vani?" , Answers: ["Jesam", "Nisam", "Budem", "Fuj"], Answer: 3)]
}

改变你的viewDidLoad:

override func viewDidLoad() {
super.viewDidLoad()
Questions = freshQuestions()
pickQuestion()
}

然后:

@IBAction func reset(sender: UIButton) {
Questions = freshQuestions()
QNumber = 0
pickQuestion() //- here is the problem, what to put here? it only continue with quiz it does not restart it, tried with reset numbers and array, but don't work
}

如果将这两行新行包装在它们自己的 resetQuiz() 函数中的重置函数中,那就更好了。

关于Swift Quiz - 重置按钮,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32606469/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com