I started with a storyboard project. and I put Menubar item. When the menubar item is clicked, the following method is triggered in AppDelegate.swift.
func setWindowVisible() {
NSApp.activateIgnoringOtherApps(ture)
NSApp.mainWindow?.makeKeyAndOrderFront(self)
}
this brings my app in front. But once I click the close button, the red one on the window, it never works.
It used to work in non-storyboard based projects no matter I close the window.
I have set
NSApp.mainWindow?.releasedWhenClosed = false
in applicationDidFinishLaunching()
Can anyone help me please?
Setting releasedWhenClosed
in applicationDidFinishLaunching
has no effect as the mainWindow
property is nil at this moment. → The window is created after this method is executed.
The releasedWhenClosed
is defaulted to false anyway when the window is created in Interface Builder.
The mainWindow property is probably nil after closing the window, because then there is no mainWindow anymore. From the docs:
The value in this property is nil when the app’s storyboard or nib file has not yet finished loading. It might also be nil when the app is inactive or hidden.
I was able to show the window again (after closing) by accessing the window from within the windows
array of NSApp
.
NSApp.activateIgnoringOtherApps(true)
NSApp.windows[0].makeKeyAndOrderFront(self)
In case you have more than one window, you need to find the right one in the array..