【问题标题】:Could not cast value of type 'UIViewController' to UITableViewController无法将“UIViewController”类型的值转换为 UITableViewController
【发布时间】:2016-01-04 21:32:57
【问题描述】:

我正在尝试从一个视图控制器转移到另一个视图控制器,并将数据传递给下一个控制器,但我不断收到此错误:

无法将 ViewController 类型的值转换为 [VC2]

设置如下:

  • NavigationController 是 VC1 的初始入口点
  • ViewController1 是一个 collectionViewController 与 VC2 的显示 segue
  • ViewController2 是一个 viewController,只有一个容器可以嵌入 VC3
  • ViewController3 是带有静态表的 tableViewController 和 集合视图。

VC3 是我的destinationViewController 并嵌入在 VC2 中。当我从 VC1 中选择 collectionViewCell 时,我得到 Signal SIGABRT。

override func collectionView(collectionView: UICollectionView, didSelectItemAtIndexPath indexPath: NSIndexPath) {

        selectedImage = UIImage(named: wholeArray[indexPath.row]["image"] as! String)!
        selectedLabel = wholeArray[indexPath.row]["name"] as? String

        self.performSegueWithIdentifier("show", sender: nil)
    }

    override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject!) {

    if (segue.identifier == "show") {
      let secondVC: ViewController02 = (segue.destinationViewController as? ViewController02)!

            secondVC.image = selectedImage
            secondVC.label = selectedLabel
        }
    }

因为UITableViewControllerUIViewController 的子类,所以这段代码应该可以正常工作。我做错了什么?

** 使用类型继承和确切错误消息更新问题: **

class ViewController01: UICollectionViewController, UICollectionViewDataSource, UICollectionViewDelegate, UICollectionViewDelegateFlowLayout {

class ViewController02: UIViewController {

class ViewController03: UITableViewController, UICollectionViewDataSource, UICollectionViewDelegate,UIScrollViewDelegate {

class HeaderView: UIView {

2015-10-07 11:26:27.384 UITableViewHeader[45474:3576139] Interface Builder 文件中的未知类 _TtC17UITableViewHeader14ViewController。

无法将 'UITableViewHeader.ViewController02' (0x107a38360) 类型的值转换为 'UITableViewHeader.ViewController03' (0x107a38a50)。

【问题讨论】:

  • 类 ContainerViewController: UIViewController
  • 请包括不同控制器的类型继承,并包括确切的错误消息,不要改写,不要重新输入,复制和粘贴它。
  • 请编辑实际问题,不要在评论中发帖。
  • 在调试器中我输入:po secondVC.image 错误:执行被中断,原因:EXC_BAD_ACCESS (code=EXC_I386_GPFLT)。进程已经返回到表达式求值前的状态。
  • 检查故事板中视图控制器的类

标签: ios iphone swift uiviewcontroller collectionview


【解决方案1】:

在您的代码中更改此行

let secondVC: ViewController02 = (segue.destinationViewController as? ViewController02)!

let secondVC:ViewController02 = segue.destinationViewController as! ViewController02

或者简单到:

    let secondVC = segue.destinationViewController as! ViewController02

【讨论】:

  • 谢谢威廉,尝试了这两种方法和多种其他变体。没有运气和同样的错误信息。
  • @mvien 错误是什么?另外请告诉我ViewControlelr02的签名
  • 用所有视图控制器和子类 uiview 的签名更新了问题。还包括确切的错误消息
  • 你在哪个课上使用prepareForSegue
  • VC1: UICollectionViewController
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2023-03-20
  • 2017-03-02
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多