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
releasedWhenClosedinapplicationDidFinishLaunchinghas no effect as themainWindowproperty is nil at this moment. → The window is created after this method is executed.The
releasedWhenClosedis 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:
I was able to show the window again (after closing) by accessing the window from within the
windowsarray ofNSApp.In case you have more than one window, you need to find the right one in the array..