Matt Spoon
Matt Spoon

Reputation: 2830

swift 2.0 prepareForSegue not working

I'm trying to use prepareForSegue in an app I am making. prepare forSegue has to pass a class to a separate view controller. Here is the code in prepareForSegue

override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
    if segue.identifier == "createPieceSegue" {
        var svc = segue.destinationViewController as DestinationViewController!

        svc.pieceBaseClass = myClass
    }
}

These are the superclasses that the first view controller inherits from:

class ViewController: UIViewController, UIPickerViewDelegate, UIPickerViewDataSource, UITableViewDataSource, UITableViewDelegate, UINavigationBarDelegate

And the destination view controller is inheriting from UIViewController only. The error I keep getting is: UIViewController is not convertible to'DestinationViewController' Thus I can't pass data between the two. What does this error mean and how can I fix it?

Upvotes: 1

Views: 1824

Answers (2)

Nicolai Schnack
Nicolai Schnack

Reputation: 36

You should be able to use optional chaining. Did you make sure that DestinationViewController actually is a subclass of the UIViewController ?

override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
if segue.identifier == "createPieceSegue" {
    if let svc = segue.destinationViewController as? DestinationViewController {
        svc.pieceBaseClass = myClass
    }
}

Upvotes: 0

Graham Perks
Graham Perks

Reputation: 23400

It means you've not set the custom class of the destination VC in your storyboard. So it's just a UIViewController and not your subclass.

Upvotes: 1

Related Questions