问题改写:
屏幕上有一些按钮显示随机问题的答案(多项选择)。看起来基本上像:
ViewDidLoad() {
some cosmetic stuff blah blah blah
nextQuestion()
}
func nextQuestion() {
display question
set button text (answers)
}
@IBAction func getAnswer(_sender: UIButton) {
check answer
do the UIAlertController thing
}
当按下按钮时,UIAlertController显示结果,然后在按下继续后,显示下一个问题和一组答案。 (只要UIAlertController被释放,是否会调用ViewDidLoad?)
除了我不想按两个按钮以进入下一个问题(所选答案和继续按钮)之外,这是有效的。按下其中一个按钮后,我想立即显示下一个问题和一组答案。
使用Xcode 9.0和Swift 4.(是的,我是Swift的新手。)
答案 0 :(得分:0)
根据您希望应用和用户互动的外观,一种方法是:
viewQ
和viewA
viewQ
将有问题和一组答案viewA
将获得问题的反馈以及“继续”按钮UIAlertController
,而是在viewA
,隐藏 viewQ
中设置反馈标签的文字, show viewA
viewQ
,隐藏 viewA
和 show {的问答元素{1}} 或者,如果你不想要"继续"按钮完全......
选择答案后,根据需要设置反馈标签的文本,并为下一个问题设置问题标签文本和答案按钮标题。
更简单:)
答案 1 :(得分:0)
以下是整个事情:
class ViewController: UIViewController {
@IBOutlet weak var displayQuestion: UILabel!
@IBOutlet weak var rightOrWrongLabel: UILabel!
@IBOutlet weak var myScore: UILabel!
@IBOutlet weak var answer1: UIButton!
@IBOutlet weak var answer2: UIButton!
@IBOutlet weak var answer3: UIButton!
@IBOutlet weak var answer4: UIButton!
@IBOutlet weak var answer5: UIButton!
override func viewDidLoad() {
super.viewDidLoad()
questions += ["q1", "q2", "q3", "q4", "q5", "q6", "q7", "q8", "q9", "q10"]
answers += ["a1", "a2", "a3", "a4", "a5", "a6", "a7", "a8", "a9", "a10"]
qIndexArray += [0, 1, 2, 3, 4, 5, 6 , 7,8 ,9]
nextQuestion()
}
func nextQuestion(action: UIAlertAction! = nil) {
// shuffle index array
qIndexArray = GKRandomSource.sharedRandom().arrayByShufflingObjects(in: qIndexArray) as! [Int]
answer1.setTitle(answers[qIndexArray[0]], for: .normal)
answer2.setTitle(answers[qIndexArray[1]], for: .normal)
answer3.setTitle(answers[qIndexArray[2]], for: .normal)
answer4.setTitle(answers[qIndexArray[3]], for: .normal)
answer5.setTitle(answers[qIndexArray[4]], for: .normal)
// generate a random Int from 0 and 4
correctAnswer = GKRandomSource.sharedRandom().nextInt(upperBound: 5)
displayQuestion.text = questions[Int(qIndexArray[correctAnswer])]
}
@IBAction func buttonTapped(_ sender: UIButton) {
var title: String
if sender.tag == Int(correctAnswer) {
title = "Correct!"
score += 1
} else {
title = "Wrong"
}
rightOrWrongLabel.text = title
myScore.text = "Current score is \(score)."
// Generate a UIAlert to display score
let alertControl = UIAlertController(title: title, message: "Current score is \(score).", preferredStyle: .alert)
alertControl.addAction(UIAlertAction(title: "Continue", style: .default, handler: nextQuestion))
present(alertControl, animated: true)
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
当我点击其中一个按钮时,它会弹出警报,等待我继续,然后加载一个新的设置。
如您所见,我已经有正确/错误的信息和分数。我想摆脱UIAlertController,以便立即显示一个新集,而无需按下继续按钮。播放20次并按10个按钮而不是20个。