I would like to show a welcome sheet when my app first launches. Starting with a blank Xcode project, I've set up a WelcomeStoryboard how I want it.
I can show it as a sheet using a button click, calling this in my ViewController
:
func showWelcome() {
let welcomeStoryboard = NSStoryboard(name: "WelcomeStoryboard", bundle: nil)
guard let welcomeViewController = (welcomeStoryboard.instantiateInitialController() as? NSWindowController)?.contentViewController else {
return
}
self.presentAsSheet(welcomeViewController)
}
However, I am finding it hard to see where I can call that so it will show the sheet when the window first loads.
Inside viewDidLoad
is too soon, it seems:
Assertion failure in -[NSViewControllerSheetTransition animatePresentationOfViewController:fromViewController:], NSViewControllerSheetTransition.m:33
Failed to set (contentViewController) user defined inspected property on (NSWindow): self.fromViewController.view.window should be valid!
I noticed that if I wrap presentAsSheet
inside DispatchQueue.main.async
it works! But I don't understand why that would work, and am reluctant to trust it.
I'm hoping someone can shed some light on this for me? It feels like a trivial requirement but I'm stuck on it!