0

I'm trying to segue from one view controller to another, and pass data to the next controller, but I keep getting this error:

Could not cast value of type ViewController to [VC2]

Setup looks like this:

  • NavigationController is initial entry point to VC1
  • ViewController1 is a collectionViewController with show segue to VC2
  • ViewController2 is a viewController with only a container to embed VC3
  • ViewController3 is a tableViewController with static table and collectionView.

enter image description here

VC3 is my destinationViewController and embedded in VC2. When I select a collectionViewCell from VC1 I get 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
        }
    }

Since UITableViewController is a subclass of UIViewController, this code should work just fine. What am I doing wrong?

** Updated question with type inheritance and exact error message: **

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).

mvien
  • 237
  • 4
  • 12
  • class ContainerViewController: UIViewController – mvien Oct 07 '15 at 15:23
  • please include the type inheritances of the different controllers and include the EXACT error message, dont rephrase, dont retype, copy&paste it. – luk2302 Oct 07 '15 at 15:26
  • please edit the actual question, dont post in the comment. – luk2302 Oct 07 '15 at 15:36
  • In debugger I entered: po secondVC.image error: Execution was interrupted, reason: EXC_BAD_ACCESS (code=EXC_I386_GPFLT). The process has been returned to the state before expression evaluation. – mvien Oct 07 '15 at 15:55
  • Check the class of the view controllers in your storyboard – Cosyn Oct 11 '15 at 12:24
  • @Cosyn Thanks. I checked the classes in SB and made sure they matched the signatures. – mvien Oct 12 '15 at 12:57
  • Try this link [prepare(for segue:) with navigation controller](https://stackoverflow.com/questions/28788416/swift-prepareforsegue-with-navigation-controller) – Jack Shih Nov 14 '17 at 07:50

1 Answers1

0

Change this line in your code

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

To

let secondVC:ViewController02 = segue.destinationViewController as! ViewController02

or simple to:

    let secondVC = segue.destinationViewController as! ViewController02
William Kinaan
  • 28,059
  • 20
  • 85
  • 118