无法转换'UINavigationController'类型的值(0x10426be10)

时间:2017-11-09 08:46:05

标签: ios swift

我是非常新的编码,我正在做一个教程,但我继续得到这个错误。不知道该怎么办。我一直在寻找答案超过一个小时。希望这里的任何人都能帮助谢谢。

 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
}

My MainStoryBoard: enter image description here

3 个答案:

答案 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