Storyboard ID Caveat

macOS 10.12, Xcode 9.2

Do not use the name of a viewController subclass as your Storyboard ID.

I’ve had both ‘Storyboard […] doesn’t contain a controller with identifier ‘MyCustomController’ and the cast to MyCustomController failing randomly; in one instance a cast to NSViewController worked, but MyCustomController did not.

When I set the identifier to “CustomVC” the same code works every time.

// working code

@IBAction func popInCode(_ sender: NSButton) {
let storyboard = NSStoryboard(name: NSStoryboard.Name(rawValue: "Main"), bundle: nil)
if let tabController = storyboard.instantiateController(withIdentifier: NSStoryboard.SceneIdentifier(rawValue: "CustomVC")) as? MyCustomController {

presentViewController(tabController, asPopoverRelativeTo: sender.bounds, of: sender, preferredEdge: .maxX, behavior: .transient)
}

}

If you replace (rawValue: "CustomVC") with (rawValue: "MyCustomController") (and make the corresponding change in the storyboard) you will, at least I will, get the random errors described above.

This post was brought to you by the question ‘can you use an NSTabViewController in a popover’ (yes… if you set your storyboard ID to something that is NOT A CLASS NAME).

Argh. Intermittent errors are the worst.