2017-12-24 8 views
-1

ボタンを押すと、FavouriteButtonHandlerでビューコントローラを表示したいと思います。エラー: 'UINavigationController'タイプの値をキャストできませんでした

私はボタンを押すと、私は次のエラーを取得する:

Could not cast value of type 'UINavigationController' (0x11177fed8)

私は、このエラーの原因を探し、それを修正する方法がわかりません。

import UIKit 

@available(iOS 11.0, *) 
class FirstARViewController: UIViewController , UICollectionViewDelegate , UICollectionViewDataSource, UICollectionViewDelegateFlowLayout { 

    @IBOutlet weak var collectionView: UICollectionView! 
    var imagescv = ["ar1","ar2" ] 

    override func viewDidLoad() { 
     super.viewDidLoad() 

     collectionView.delegate = self 
     collectionView.dataSource = self 
    } 

    func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int { 
     return imagescv.count 
    } 

    func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell { 

     let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "cell", for: indexPath) as! cellimagesar 

     cell.layer.cornerRadius = 5 

     cell.layer.borderWidth = 1 

     cell.myImages.image = UIImage(named: imagescv [indexPath.row]) 
     cell.myImages.contentMode = .scaleToFill 

     cell.buttonMove.tag = indexPath.item 
     cell.buttonMove.addTarget(self, action: #selector(self.FavouriteButtonHandler), for: .touchUpInside) 
     //Declaring cell 

     return cell 
    } 
    func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize { 
     return CGSize(width: 171, height: 250) 
    } 

    @objc func FavouriteButtonHandler (sender: UIButton) 
    { 
     let VcToPresent = self.storyboard?.instantiateViewController(withIdentifier: "PDFViewController") as! PDFViewController 

     switch sender.tag { 
     case 0: 
      //here you selected Button with tag 0 

      //Time to update value of Global Struct 
      ButtonSelected.Tag = 0 

      //Time to present controller that will handle your pdf file and view it 

     self.navigationController?.pushViewController(VcToPresent, animated: true) 

      break 
     case 1: 
      //here you selected Button with tag 1 

      //Time to update value of Global Struct 
      ButtonSelected.Tag = 1 
        //Time to present controller that will handle your pdf file and view it 
     self.navigationController?.pushViewController(VcToPresent, animated: true) 

      break 
     default: 
      print("Error case") 
     } 
    } 
} 

そして私は、この行でエラーが出ます:

ビューコントローラのコードは次の

let VcToPresent = self.storyboard?.instantiateViewController(withIdentifier: "PDFViewController") as! PDFViewController 
+0

「iavailable(iOS 11.0、*)」という11行の連続する行の目的は何ですか? – vadian

+0

私は質問の可読性を改善しようとしましたが、また空白行のシリーズを削除しました。それぞれ前のものよりも白いものがありました –

答えて

0

それは私のために完璧に働いています。ナビゲーションスタックにPDFViewControllerを押す

if let presentVC = self.storyboard?.instantiateViewController(withIdentifier: "PDFViewController") as? PDFViewController{ 
     self.navigationController?.pushViewController(presentVC, animated: true) 
    } 

OR

if let presentVC = UIStoryboard(name: "Main", bundle: nil).instantiateViewController(withIdentifier: "PDFViewController") as? PDFViewController { 
     self.navigationController?.pushViewController(presentVC, animated: true) 
    } 

、あなたの現在のViewControllerがそれに何かをプッシュする前に、ナビゲーションスタックの最上位に属している必要があることを確認してください。

+0

このコードは何もしません –

関連する問題