Search code examples
macoscocoaappdelegatexcode10swift4.2

How to make a specific window have privilege to quit the app after closing its last window but not the whole app?


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


Solution

  • I figured out a way to achieve this by implementing the applicationShouldTerminateAfterLastWindowClosed in AppDelegate with return true, and then by implementing windowShouldClose(_sender:) in all view controller classes that I want to determine whether the window should close (Application should quit since the applicationShouldTerminateAfterLastWindowClosed is always returning true) when the window receives the performClose(_:) message. And now, everything works perfectly.