ios - 无法将类型 'UIViewController' 的值转换为 UITableViewController

标签 ios iphone swift uiviewcontroller collectionview

我试图从一个 View Controller 切换到另一个 View Controller ,并将数据传递到下一个 Controller ,但我不断收到此错误:

Could not cast value of type ViewController to [VC2]

设置如下:

  • NavigationController 是 VC1 的初始入口点
  • ViewController1 是一个 collectionViewController,显示转场至 VC2
  • ViewController2 是一个 viewController,只有一个嵌入 VC3 的容器
  • ViewController3 是一个带有静态表格的 tableViewController Collection View 。

enter image description here

VC3 是我的 destinationViewController 并嵌入到 VC2 中。当我从 VC1 选择一个 collectionViewCell 时,我收到信号 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] Unknown class _TtC17UITableViewHeader14ViewController in Interface Builder file.

Could not cast value of type 'UITableViewHeader.ViewController02' (0x107a38360) to 'UITableViewHeader.ViewController03' (0x107a38a50).

最佳答案

更改代码中的这一行

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

let secondVC:ViewController02 = segue.destinationViewController as! ViewController02

或者简单地:

    let secondVC = segue.destinationViewController as! ViewController02

关于ios - 无法将类型 'UIViewController' 的值转换为 UITableViewController,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32995837/

相关文章:

iphone - 使用 segue 和 Storyboard从一个 Controller 移动到另一个 Controller

ios - 使用坐标和 NSPredicate 过滤 MKAnnotations 的 NSSet

ios - 如何在 swift 中解析 json(将 json 字符串转换为字符串)

iphone - 如何在派生类中分配全局 NSInteger 对象

ios - Swift 3 - 为每个设备设置 X-Y 轴 UILabel

ios - Socket.io 客户端在突然断开连接后无法重新连接到服务器

android - 与 Apportable 交叉编译相机应用程序

ios - 获取任何 AudioPlayer 来传输或播放音乐

ios - 快捷按钮概述

ios - 在 MKMapView 中计算方位在穿过 180 度子午线时给出错误值