我正在尝试从初始视图控制器向我的Username VC
发送多个对象。这是来自我的控制器的segue代码:当我添加代码以发送第二个对象termreport
时,会出现问题。如果我删除termsM
和作业,它会像往常一样发送学生,但我还需要发送termReport
对象。我该如何解决这个问题?
ViewControler :
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?
UsernameVC :
var terms: [TermReport]!
override func viewDidLoad() {
print("TERM \(terms[0].grades[3])")//returns found nil optional ERROR
}
答案 0 :(得分:2)
您必须使用segue将要发送到另一个ViewController
的所有变量包含到单个对象(也可以是一个集合)中。您可以创建具有类型[Student]
和[TermReport]
的属性的自定义类/结构,也可以将它们放入本机集合(元组或字典)。
创建自定义结构:
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)