我無法將數據發送到第二個視圖控制器。更具體地說,我解析一個網站,並將特定的元素放入一個對象中。從那裏我試圖將響應發送到另一個視圖控制器。我在應用程序中已經做了一次,但是我不確定這次我做錯了什麼。Swift將數據通過Segue發送到ViewController問題
VC1:
override func prepare(for segue: UIStoryboardSegue, sender: Any!) {
guard let assignments = sender as AnyObject as? [Assignment],
let sendMe = segue.destination as? ClassTableViewController
else { return }
sendMe.myAssignments = assignments
}
let classResponse = try ClassParser(innerHTML)
self.performSegue(withIdentifier: "ShowClass", sender: classResponse.classAssignments) //segue to ClassTableViewController
ClassTableViewController使用該對象來顯示詳細信息。但是,錯誤不斷以unexpectedly found nil while unwrapping an Optional value.
的形式返回,這意味着對象內容未被正確發送。 ClassTableViewController:
class ClassTableViewController: HeaderViewController {
@IBOutlet weak var classTableView: UITableView!
var myAssignments: [Assignment]!
override func viewDidLoad() {
}
}
extension ClassTableViewController: UITableViewDataSource, UITableViewDelegate {
func numberOfSections(in tableView: UITableView) -> Int {
// #warning Incomplete implementation, return the number of sections
return 1
}
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return myAssignments.count //ERROR HERE. The variable is empty
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "ClassViewCell", for: indexPath)// as! NineWeekCellTableViewCell
cell.textLabel?.text = myAssignments[indexPath.row].name
cell.detailTextLabel?.text = myAssignments[indexPath.row].grade
return cell
}
}
您的錯誤不會發生在這些代碼行中。請發佈錯誤發生的代碼 –
@FangmingNing好吧,我添加了第二個控制器上面 – yzet00