Why can't I cast to my viewcontroller subclass within a storyboard?

277 Views Asked by At

I have a storyboard where I have a window controller and a view controller. The most reasonable way to access the view controller within code is via an accessor defined in the owning window controller.

class TutorialHUDWindowController: NSWindowController {

    var tutorialHUDViewController: TutorialHUDViewController {
        get {
            return self.contentViewController! as! TutorialHUDViewController
        }
    }

}

Everything compiles fine, but when trying to access the view controller during runtime, the app crashes with a sigabrt:

enter image description here

and the console logs error messages similar to these:

MyApp[6178:329283] Unknown class TutorialHUDViewController in Interface Builder file at path MyApp/Build/Products/debug/Myapp/Contents/Resources/TutorialHUDStoryboard.storyboardc/NSWindowController-scR-w0-00.nib.
MyApp[6178:329283] Failed to connect (otherView) outlet from (NSViewController) to (otherView): missing setter or instance variable
Could not cast value of type 'NSViewController' (0x7fff89ae9410) to 'MyApp.TutorialHUDViewController' (0x1001f6a38).
Myapp[6178:329283] Could not cast value of type 'NSViewController' (0x7fff89ae9410) to 'Thimble.TutorialHUDViewController' (0x1001f6a38).

I've used a similar casting pattern in other places of my app and things work fine, what's going wrong?

1

There are 1 best solutions below

0
Vivek Gani On

In your storyboard, the custom class viewcontroller reference should also reference the appropriate module it's in. Either set it to 'MyApp' if that's where it is or use the 'Inherit From Target' selection.

enter image description here

Also, you might need to 'clean' before building again to clear out any cached storyboard compilation.