2017-11-09 9 views
0

私はコーディングに非常に慣れています。私はチュートリアルを行っていますが、このバグは継続しています。何をすべきかわからない。私は1時間以上答えを探していました。誰でもここに感謝を助けることを願っています。タイプ 'UINavigationController'(0x10426be10)の値をキャストできませんでした

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 
} 

マイMainStoryBoard: enter image description here

+0

https://stackoverflow.com/questions/45688834/could-not-cast-value-of-typeに定義されていることを確認し

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 } } 

を-uinavigationcontroller-to-jacrs-ios-topviewcontr – Bapu

答えて

0

このような何か試してみてください。また、あなたのセグエidentifier

0

はおそらく、あなたのセグエはあなたのDetailViewController内でUINavigationControllerを表示しようとしています。
はそれを行うようにしてください:ここでは

let destination = (segue.destination as! UINavigationController).rootViewController as! DetailViewController 
0

:あなたはDetailViewControllerUINavigationControllerをキャストしようとしている

let destination = segue.destination as! DetailViewController 

。これは達成できません。 DetailViewController(あなたのスクリーンショットから)はUINavigationControllerで管理されています。これはあなたの場合の直接の宛先です。

最初のコントローラをUINavigationControllerにラップすることをおすすめします。つまり、Navigation Controllerをアプリの開始点にしてから、テーブルからDetailViewController(中間のナビゲーションコントローラではなく)にセグメントを描画します。その後、あなたのコードは正常に動作します。