Xcode 8 - Problem with View Controllers initialized in storyboard

If I try to instantiate a view controller from the storyboard by identifier, I get a UIViewController back instead of my subclass MyViewController.


On launch the app crashes when trying to load the vc's from the storyboard. There is a UIRuntimeOutletConnection that appears to be doing a setValueForKey that is throwing an exception.


This works fine in Xcode 7.


I finally got the project to build after migrating to swift 3, but I can't get much further now.


Why isn't my custom vc being instantiated?


I am running in the simulator and I'm wondering if that could be the problem?

Replies

Click on the View Controller in the storyboard. Look in the Identity Inspector on the right. Under Custom Class, make sure the Class setting is MyViewController instead of UIViewController. In you swift code, you will need to cast the result of instantiateViewController to MyViewController:

self.myVC = UIStoryboard(name: "Main", bundle: nil).instantiateViewController(withIdentifier: "myVC") as! MyViewController

Running on the simulator is not the problem.

Yes this is what I do my issue is it is not an instance of my class. It is fine in Xcode 7. Ill go ahead and file a bug.

Found the issue:


The view controller in my storyboard is a subclass of a CoreDataCollectionViewController - simply a class which handles the NSFetchedResultsController delegate methods for a collection view. Pretty typical.


Because NSFetchedResultsController must now specify it's entity type (as a generic type parameter), I had to add a generic parameter to my superclass:


class CoreDataCollectionViewController<Entity:NSManagedObject>: UIViewController, NSFetchedResultsControllerDelegate {
    @IBOutlet weak var collectionView: UICollectionView!
    var fetchedResultsController : NSFetchedResultsController<Entity>
}


Now in my subclass I had to add a generic type as well:


class MyViewController: CoreDataCollectionViewController<NasaPicture>


The storyboard couldn't handle instantiating a view controller of this type and returned a plain UIViewController.


I'll file a bug report as it compiles OK but crashes at run time.