'Show sheet when the main window first loads
I am trying to get a sheet to load when the main window first loads. This sheet is so that it asks the user for a file so that they are sort of forced to open a file for use in the program when they first start.
I tried to put performSegue(withIdentifier: sender:) in viewDidLoad(), however it just loads the sheet and nothing else.
override func viewDidLoad() {
super.viewDidLoad()
performSegue(withIdentifier: NSStoryboardSegue.Identifier(rawValue: "browse"), sender: self)
}
I want the main window to load, then the sheet to slide down immediately after asking the user to open a file.
Where should I put the performSegue or is there another way I should be doing this?
Solution 1:[1]
@mschmidt is right, I believe; you'll need to perform the segue from viewDidAppear()
or windowDidBecomeMain()
or similar; the view needs to have been displayed before you can segue from it.
Note that viewDidAppear()
will fire when the view is drawn, whether or not it has focus; windowDidBecomeMain()
will only fire when your app becomes the frontmost app; you may be working in another app but still be able to see your app's windows on-screen, and in this state windowDidBecomeMain()
may not have fired yet.
And yes, both viewDidAppear()
and windowDidBecomeMain()
will fire every time the view appears or the window becomes main, meaning if you minimise the app then un-minimise it, both will fire again. So you'll need some way of ensuring your segue doesn't show again each time, unless you want it to. A simple boolean flag eg segueHasShown
should achieve that.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|---|
Solution 1 | jeff-h |