错误:无法转换类型'UINavigationController'的值 [英] Error: Could not cast value of type 'UINavigationController'
本文介绍了错误:无法转换类型'UINavigationController'的值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我想在按下按钮时在FavouriteButtonHandler中显示一个视图控制器。
I want to present a view controller in FavouriteButtonHandler on a button press.
当我按下按钮时,我收到以下错误:
When I press the button, I get the following error:
无法转换'UINavigationController'类型的值(0x11177fed8)
I搜索此错误的原因,但不知道如何解决它。
I searched for the cause of this error but do not know how to fix it.
查看控制器代码如下:
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")
}
}
}
我收到了这个错误line:
And I get the error in this line :
let VcToPresent = self.storyboard?.instantiateViewController(withIdentifier: "PDFViewController") as! PDFViewController
推荐答案
这对我来说非常有用:
将PDFViewController推入导航堆栈:
Pushing the PDFViewController into Navigation Stack :
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在推送任何内容之前应该属于Navigation堆栈的顶部。
Please, make sure that your current viewController should belong to the top of Navigation stack before pushing anything on it.
这篇关于错误:无法转换类型'UINavigationController'的值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文