gpt4 book ai didi

arrays - Segue 不传输数组

转载 作者:行者123 更新时间:2023-11-30 14:19:05 24 4
gpt4 key购买 nike

我似乎无法理解为什么每当我运行测试程序时数组答案都是空的。

当我在 View Controller 1 中使用 println(finalResultsSegue) 时,我可以看到结果数组,但是当我将其传递给 View Controller 2 中的答案时,它会转到下一个屏幕,但数组仍为空。

View Controller 1

@IBAction func submitAll(sender: UIButton) {

let hasAnsweredAllQuestions = questions.reduce(true) { (x, q) in x && (q.usersAnswer != nil) }

println("has user answered all questions?: \(hasAnsweredAllQuestions)")

if hasAnsweredAllQuestions == true {

for (index, _) in enumerate(questions) {
userAnswers.append(questions[index].usersAnswer!)
correctAnswers.append(questions[index].answer!)
}
if userAnswers.count == 3 {
checkResults()
}
}
}
}

func checkResults() {
let answer = map(zip(correctAnswers, userAnswers)){
($0.0 == $0.1) ? "correct" : "wrong"
}
var finalResults = answer.map({"The following answer is \($0)"})

finalResultsSegue = finalResults

println(finalResultsSegue)
}

@IBAction func nextPage(sender: AnyObject) {

self.performSegueWithIdentifier("answerSummary", sender: sender)

func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject!) {

if(segue.identifier == "answerSummary") {
var answerSummary = segue.destinationViewController as! QuizResultsViewController

answerSummary.answers = finalResultsSegue
}
}
}

View Controller 2

class QuizResultsViewController: UIViewController {

@IBOutlet weak var testLabel: UILabel!

var answers: [String] = []

override func viewDidLoad() {
super.viewDidLoad()

// Do any additional setup after loading the view.

println(answers)

}

最佳答案

您已将 prepareForSegue 作为 nextPage 内的嵌套函数。将其移到外面,它应该可以正常工作:

@IBAction func nextPage(sender: AnyObject) {
self.performSegueWithIdentifier("answerSummary", sender: sender)
}

override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject!) {
if let answerSummary = segue.destinationViewController as? QuizResultsViewController
where segue.identifier == "answerSummary"

answerSummary.answers = finalResultsSegue
}
}

事实上,您不必在 func prepareForSegue... 前面添加 override ,这暗示您已将 prepareForSegue 放入错误的地方。

关于arrays - Segue 不传输数组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30708062/

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