我是非常新的编码,我正在做一个教程,但我继续得到这个错误。不知道该怎么办。我一直在寻找答案超过一个小时。希望这里的任何人都能帮助谢谢。
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
let indexPath = self.tableView.indexPath(for: sender as! UITableViewCell)!
let contact = self.contacts[indexPath.row]
let destination = segue.destination as! DetailViewController
destination.contact = contact
}
答案 0 :(得分:0)
最有可能的是,您的segue正试图在UINavigationController
内显示DetailViewController
尽量做到这一点:
let destination = (segue.destination as! UINavigationController).rootViewController as! DetailViewController
答案 1 :(得分:0)
下面:
let destination = segue.destination as! DetailViewController
您正在尝试将UINavigationController
投射到DetailViewController
。这是无法实现的。 DetailViewController
(来自您的屏幕截图)由UINavigationController
管理,UINavigationController
是您的直接目的地。
因此,最佳做法是将初始控制器包装到DetailViewController
,即只需将导航控制器作为应用程序的起点,然后将segue从表格绘制到var child = element[0].firstElementChild;
(而不是中间导航控制器) 。之后,您的代码将正常运行。
答案 2 :(得分:0)
尝试这样的事情:
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
if segue.identifier == "yourIdentifier" {
let indexPath = self.tableView.indexPath(for: sender as! UITableViewCell)!
let contact = self.contacts[indexPath.row]
let destination = segue.destination as! UINavigationController
let vc = destination.topViewController as! DetailViewController
vc.contact = contact
}
}
还要确保定义了identifier