gpt4 book ai didi

ios - 通过 segue 传递错误的数据

转载 作者:行者123 更新时间:2023-11-28 05:48:49 24 4
gpt4 key购买 nike

我正在尝试为我的测验随机分配问题,并在答案回答不正确时显示 segue。我认为这可能与我的 NewNumber() 有关,但我无法弄清楚。

有时它适用于前 3 个问题,但即使通过按钮选择了正确答案,也会触发 segue。

所以,我喜欢的是:- 随机问题(没有重复,但我不得不摆脱 list.remove 因为它搞砸了正确答案 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
}
}


}

最佳答案

newNumber 方法中,您分配了 questionNumber,但是在准备 segue 时,您分配了 oldQNumber 作为 vc1 的变量。因为你在调用 newNumber 后没有更改 oldQNumber,所以我想你想传递 questionNumber

vc1.InformationRoundNumber = questionNumber

关于ios - 通过 segue 传递错误的数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53706164/

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