0
votes

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

2

2 Answers

0
votes

The docs for applicationShouldTerminateAfterLastWindowClosed say that it's called at the time the decision is being made (each time):

The application sends this message to your delegate when the application’s last window is closed.

That lets you give a different answer in different circumstances.

(You might have thought that it would be called only once early in the app lifetime and dictate the behavior for the whole life of the app.)

So, you can communicate the fact that you're doing a window transition (or otherwise plan to soon open a window) to your app delegate. It can return false from applicationShouldTerminateAfterLastWindowClosed so long as that's the case. Of course, you have to tell it to return to normal behavior after you've done the transition.

0
votes

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.