Can't cast value of type UIViewController to PatternDetailViewController Can't cast value of type UIViewController to PatternDetailViewController ios ios

Can't cast value of type UIViewController to PatternDetailViewController


The problem, as you have said, is in these lines:

if segue.identifier == "patternDetailSegue" {    var detailViewController = segue.destinationViewController as! PatternDetailViewController     // Could not cast value of type 'UIViewController' to 'Patternz.PatternDetailViewController'

The error message tells you that the destinationViewController of this segue is not, in fact, a PatternDetailViewController. You may think it is, but it isn't. You need to examine this segue in the storyboard and see what's really at the destination end of it.

The fact that the error message describes it as a UIViewController makes me suspect that you forgot to enter any view controller type in this view controller's Identity inspector in the storyboard:

Xcode Screenshot


Adding to matt answer. Sometimes, you may also need to double check the custom class Module from the Identity inspector.

enter image description here

Your view controller need to belong to a specific Module. So make sure to select a Module from the list. In my case:

enter image description here


It looks like you are using a navigation controller judging by your viewDidLoad() in PatternDetailViewController.

If PatternDetailViewController is embedded in a UINavigatonController then the navigation controller will be segue.destinationViewController.

Get the PatternDetailViewController like this:

let vc: UINavigationController = segue.destinationViewController as! UINavigationControllerlet detailVC = vc.topViewController as! PatternDetailViewControllerdetailVC.pattern = self.selectedPattern


matomo