创建问答游戏
我该如何补充,当答案不正确时,错误答案会变成红色,而正确答案会同时变成绿色?
当出现新问题时,如何使颜色消失?我认为,当您按下答案时,之后会出现一个新问题
编辑:此代码工作正常。
@IBOutlet var options: [UIButton]!
@IBOutlet weak var questionLabel: UILabel!
@IBOutlet weak var progressView: UIView!
var allQuestions = QuestionBank()
var Number: Int = 0
var selectedAnswer: Int = 0
override func viewDidLoad() {
super.viewDidLoad()
oppdatertekst()
options.forEach {
$0.layer.cornerRadius = 20
$0.backgroundColor = UIColor.orange
$0.setTitleColor(UIColor.black, for: .normal)
}
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
@IBAction func answerPressed(_ sender: UIButton) {
feedback()
if sender.tag == selectedAnswer {
sender.backgroundColor = UIColor.green
let riktig = NSLocalizedString("Quiz.riktig", comment: "")
ProgressHUD.showSuccess(riktig)
} else if let correctOption = options.first(where: { $0.tag == selectedAnswer }) {
let feilnr = NSLocalizedString("Quiz.feilnr", comment: "")
ProgressHUD.showError("\(feilnr)\(selectedAnswer)")
correctOption.backgroundColor = UIColor.green
sender.backgroundColor = UIColor.red
}
DispatchQueue.main.asyncAfter(deadline: .now() + 5) {
self.Number += 1
self.oppdatertekst()
}
}
func oppdaterSpm() {
if Number <= allQuestions.list.count - 1{
questionLabel.text = allQuestions.list[Number].question
options.forEach {
$0.backgroundColor = .white
}
options[0].setTitle(allQuestions.list[Number].optionA, for: .normal)
options[1].setTitle(allQuestions.list[Number].optionB, for: .normal)
options[2].setTitle(allQuestions.list[Number].optionC, for: .normal)
options[3].setTitle(allQuestions.list[Number].optionD, for: .normal)
selectedAnswer = allQuestions.list[Number].correctAnswer
} else {
let alert....
}
}
答案 0 :(得分:0)
不是使用四个IBOutlet
,而是使用IBOutletCollection
并将这四个按钮连接到此集合。
在answerPressed
方法中,如果选择了正确答案,则将单击的按钮颜色更改为绿色。如果选择了错误的答案,则将选定的答案颜色更改为红色,然后从集合中获取正确的答案按钮,并将其颜色更改为绿色。 5秒后,重新加载下一个问题。
class ViewController: UIViewController {
@IBOutlet var options: [UIButton]!
@IBOutlet weak var questionLabel: UILabel!
@IBOutlet weak var progressView: UIView!
var allQuestions = QuestionBank()
var Number: Int = 0
var selectedAnswer: Int = 0
override func viewDidLoad() {
super.viewDidLoad()
oppdatertekst()
options.forEach {
$0.layer.cornerRadius = 20
$0.backgroundColor = UIColor.orange
$0.setTitleColor(UIColor.black, for: .normal)
}
}
@IBAction func answerPressed(_ sender: UIButton) {
feedback()
if sender.tag == selectedAnswer {
sender.backgroundColor = UIColor.green
let riktig = NSLocalizedString("Quiz.riktig", comment: "")
ProgressHUD.showSuccess(riktig)
} else if let correctOption = options.first(where: { $0.tag == selectedAnswer }) {
let feilnr = NSLocalizedString("Quiz.feilnr", comment: "")
ProgressHUD.showError("\(feilnr)\(selectedAnswer)")
correctOption.backgroundColor = UIColor.green
sender.backgroundColor = UIColor.red
}
DispatchQueue.main.asyncAfter(deadline: .now() + 5) {
Number += 1
oppdatertekst()
}
}
}
通过oppdaterSpm
方法更改所有按钮的颜色
func oppdaterSpm() {
if Number <= allQuestions.list.count - 1{
questionLabel.text = allQuestions.list[Number].question
options.forEach {
$0.backgroundColor = .white
}
options[0].setTitle(allQuestions.list[Number].optionA, for: .normal)
options[1].setTitle(allQuestions.list[Number].optionB, for: .normal)
options[2].setTitle(allQuestions.list[Number].optionC, for: .normal)
options[3].setTitle(allQuestions.list[Number].optionD, for: .normal)
selectedAnswer = allQuestions.list[Number].correctAnswer
} else {
let alert....
}
}
答案 1 :(得分:0)
您必须使用通用方法编写UIButton
项更改。随处呼叫。
出现新问题时:
func whenNewQuestionComes() {
optionA.layer.cornerRadius = 20
optionA.backgroundColor = UIColor.orange
optionA.setTitleColor(UIColor.black, for: .normal)
optionB.layer.cornerRadius = 20
optionB.backgroundColor = UIColor.orange
optionB.setTitleColor(UIColor.black, for: .normal)
optionC.layer.cornerRadius = 20
optionC.backgroundColor = UIColor.orange
optionC.setTitleColor(UIColor.black, for: .normal)
optionD.layer.cornerRadius = 20
optionD.backgroundColor = UIColor.orange
optionD.setTitleColor(UIColor.black, for: .normal)
}
显示绿色和红色
@IBAction func answerPressed(_ sender: UIButton) {
feedback()
optionA.backgroundColor = UIColor.red
optionB.backgroundColor = UIColor.red
optionC.backgroundColor = UIColor.red
optionD.backgroundColor = UIColor.red
if sender.tag == selectedAnswer {
sender.backgroundColor = UIColor.green
let riktig = NSLocalizedString("Quiz.riktig", comment: "")
ProgressHUD.showSuccess(riktig)
}
/*
else if let correctOption = options.first(where: { $0.tag == selectedAnswer }) {
let feilnr = NSLocalizedString("Quiz.feilnr", comment: "")
ProgressHUD.showError("\(feilnr)\(selectedAnswer)")
correctOption.backgroundColor = UIColor.green
sender.backgroundColor = UIColor.red
}
*/
DispatchQueue.main.asyncAfter(deadline: .now() + 5) {
self.Number += 1
self.oppdatertekst()
}
}