我正在尝试将测验中的问题随机化,如果答案回答不正确,则会出现提示。我认为这可能与我的NewNumber()有关,但我无法弄清楚。
有时它适用于前三个问题,但是即使通过按钮选择了正确答案,也会触发segue。
所以,我想要的是: -随机问题(不再重复,但我不得不删除列表。删除它,因为它使正确答案更加混乱) -如果输入的答案有误,我希望它触发带有正确对应信息的另一个屏幕的搜索
我列出了完整的代码,因为我不知道它是否也与我的命令或功能有关。
class RoundThree: UIViewController {
@IBOutlet weak var FuelLevel: UIImageView!
@IBOutlet weak var QuestionLabel: UILabel!
@IBOutlet weak var Car: UIImageView!
@IBOutlet weak var SmallTown: UIImageView!
//Outlet for Buttons
@IBOutlet weak var OptionA: UIButton!
@IBOutlet weak var OptionB: UIButton!
@IBOutlet weak var OptionC: UIButton!
let allQuestions2 = QuestionBank2()
var questionNumber: Int = 0
var oldQNumber: Int = 0
var score: Int = 0
var selectedAnswer: Int = 0
var fuelleackage: CGFloat = 0
var wronganswer: Int = 0
let screenSize: CGRect = UIScreen.main.bounds
var questionsasked = [Int]()
override func viewDidLoad() {
newNumber()
startQuiz()
super.viewDidLoad()
}
@IBAction func answerPressed(_ sender: UIButton){
if sender.tag == selectedAnswer {
Car.frame.origin.x += 40
score += 1
fuelleackage += 0.0
NewYorkCity()
} else if sender.tag != selectedAnswer {
fuelleackage += 40.0
wronganswer += 1
EducateYourself()
NewYorkCity()
}
updateUI()
}
func updateQuestion(){
QuestionLabel.text = allQuestions2.list[oldQNumber].question
OptionA.setTitle(allQuestions2.list[oldQNumber].optionA, for: UIControl.State.normal)
OptionB.setTitle(allQuestions2.list[oldQNumber].optionB, for: UIControl.State.normal)
OptionC.setTitle(allQuestions2.list[oldQNumber].optionC, for: UIControl.State.normal)
selectedAnswer = allQuestions2.list[oldQNumber].CorrectAns
}
func updateUI(){
FuelLevel.frame.size.width = screenSize.width * (1.0-0.15 * CGFloat(wronganswer))
oldQNumber = questionNumber
newNumber()
updateQuestion()
questionsasked.append(questionNumber)
}
//Carol.frame.origin.x += 30
func startQuiz (){
score = 0
updateQuestion()
updateUI()
}
func NewYorkCity(){
if (Car.frame.origin.x + (Car.frame.width / 2)) > (SmallTown.frame.origin.x + SmallTown.frame.width) {
performSegue(withIdentifier: "FinishWindow", sender: self)
}
else if (screenSize.width * (1.0-0.15 * CGFloat(wronganswer)) <= 0)
{ performSegue(withIdentifier: "saaaad", sender: self)
}
}
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
if (segue.identifier == "PopUp2") {
let vc1 = segue.destination as! CorrectInfoPopUp2
vc1.InformationRoundNumber = oldQNumber
}
}
func EducateYourself(){
performSegue(withIdentifier: "PopUp2", sender: self)
}
func newNumber() {
for _ in 0..<allQuestions2.list.count {
var rand = Int(arc4random_uniform(UInt32(allQuestions2.list.count)))
while questionsasked.contains(rand) {
rand = Int(arc4random_uniform(UInt32(allQuestions2.list.count)))
}
questionNumber = rand
}
}
}
答案 0 :(得分:1)
在newNumber
方法中,您分配了questionNumber
,但是在准备segue时,您分配了oldQNumber
作为vc1
的变量。因为您在致电oldQNumber
之后没有更改newNumber
,所以我想您想通过questionNumber
vc1.InformationRoundNumber = questionNumber
答案 1 :(得分:0)
答案与Robert Dresler相同。
以及几个附加组件:
示例:
struct SegueIdentifiers {
static let popUp = "PopUp"
static let finishWindow = "FinishWindow"
}
此演员表并不十分安全:
if (segue.identifier == "PopUp2") {
let vc1 = segue.destination as! CorrectInfoPopUp2
vc1.InformationRoundNumber = oldQNumber
}
所以写这样的东西好一点:
if segue.identifier == SegueIdentifiers.popUp, let vc = segue.destination as? CorrectInfoPopUp2 {
vc.InformationRoundNumber = questionNumber
}
Int.random(0..<allQuestions2.list.count)
代替Int(arc4random_uniform(UInt32(allQuestions2.list.count)))
P.S。如果您对如何编写更具可读性的代码感兴趣,则可以查看Raywenderlich's Swift Style Guide。