I have multiple window controllers in my app, but I want only some of the window controllers to quit the app after the last window closed.
I tried adding applicationShouldTerminateAfterLastWindowClosed(_:)
in the AppDelegate.swift
file:
func applicationShouldTerminateAfterLastWindowClosed(_ sender: NSApplication) -> Bool {
return true
}
But it seems like it works for the whole application to quit the app after last window closed.
The reason why I want to achieve this is that some of my transitions in my app include closing the window and showing a new window using self.view.window!.close()
or NSApp.mainWindow!.close()
, once these functions are called, the app quit immediately. So, I don't want applicationShouldTerminateAfterLastWindowClosed(_:)
to be applied for the entire application.
I tried to use exampleWindowController.showWindow(self)
before calling functions to close the previous window, but still, it didn't work, it still terminated my app. I also tried to use windowShouldClose(_:)
from NSWindowDelegate
to tell the app to quit after the window closed for a specific window controller (I probably have some misunderstanding on how to properly use this function):
extension exampleWindowController: NSWindowDelegate {
func windowWillClose(_ notification: Notification) {
NSApp.terminate(self)
}
}
But still, nothing happened...
So I'm asking is there any way to achieve this?
If not, then I ask if there is any way to tell the app not to quit at a specific time when I'm programmatically closing the window?
P.S. I'm using Xcode 10 with Swift 4.2 Cocoa API