gpt4 book ai didi

ios - Swift 将多个对象发送到 View Controller

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

我正在尝试将多个对象从我的初始 View Controller 发送到我的 Username VC。这是来 self 的 Controller 的 segue 代码:当我添加代码以发送第二个对象 termreport 时,问题就来了。如果我删除 termsM 和作业,它会像往常一样发送学生,但我还需要发送 termReport 对象。我该如何解决这个问题?

View Controller :

override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
guard let students = sender as AnyObject as? [Student]
else { return }
guard let termsM = sender as AnyObject as? [TermReport] //How would I send both objects?
else { return }
if let secondVC = segue.destination as? UsernameVC {
secondVC.students = students
secondVC.userWebView = webView
secondVC.terms = termsM // not sending
}
let gradeResponse = try Parser(innerHTML)
self.performSegue(withIdentifier: "ShowStudents", sender: gradeResponse.students)
self.performSegue(withIdentifier: "ShowStudents", sender: gradeResponse.termReports) //how would I send both variables?

用户名VC:

var terms: [TermReport]!
override func viewDidLoad() {
print("TERM \(terms[0].grades[3])")//returns found nil optional ERROR
}

最佳答案

您必须使用 segue 将要发送到另一个 ViewController 的所有变量包含到单个对象(也可以是集合)中。您可以创建一个具有类型为 [Student][TermReport] 的属性的自定义类/结构,或者将它们放入 native 集合(元组或字典)中。

创建自定义结构:

struct TermData {
var students = [Student]()
var termReports = [TermReport]()
}
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
guard let segueData = sender as? TermData
else { return }
if let secondVC = segue.destination as? UsernameVC {
secondVC.students = segueData.students
secondVC.userWebView = webView
secondVC.terms = segueData.termReports
}
}

let gradeResponse = try Parser(innerHTML)
let termData = TermData(students: gradeResponse.students, termReports: gradeResponse.termReports)
self.performSegue(withIdentifier: "ShowStudents", sender: termData)

关于ios - Swift 将多个对象发送到 View Controller ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44850357/

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