swift - Edit- How can I add that when the answer is wrong it will turn red and green in the correct answer? Swift 4.2
问题描述
Creates a quiz game
How can I add that when the answer is incorrect, the incorrect answer will turn red and the correct answer will turn green at the same time?
How can I make the colors disappear when the new question comes? I have it that when you press an answer, a new question will come right after that
EDIT: This code is working fine.
@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....
}
}
解决方案
Instead of having four IBOutlet
s use IBOutletCollection
and connect these four buttons to this collection.
In answerPressed
method if the correct answer is selected change clicked button color to green. If the wrong answer is selected change selected answer color to red, then get the correct answer button from the collection and change its color to green. After 5 seconds reload next question.
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()
}
}
}
Change all buttons color in oppdaterSpm
method
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....
}
}
推荐阅读
- javascript - 为什么我的链接在点击后会短暂变为红色?
- typescript - Typescript Types:可用作索引签名的对象值的类型
- c++11 - 未解决的 std::__cxx11::basic_string::~basic_string 和 std::allocator
::~allocator() 符号 - kubernetes - Kubernetes 配置文件中的“|-”是什么意思
- vue.js - 由于非常不清楚的原因,铬以可预测的方式半冷冻
- c# - 在签名相关但不相同的接口之间进行强制转换是一种反模式吗?
- c# - 如果值为字符串,则 ASP.NET Core SelectList 不绑定
- python-3.x - 无法导入 vxlapi:找不到函数“xlCanReceive”
- laravel - 如何在 Laravel 5.8 中对登录执行添加操作?
- ios - 如何修复“不变违规:requireNativeComponent:在 UIManager 中找不到“RNGADBannerView”?