Trying to Know When a Window Closes in a MACos Document Based Application

Trying to know when a window closes in a macOS Document based application

The problem there is that the window property will always return nil inside viewDidLoadMethod. You need to set the delegate inside viewWillAppear method:

class ViewController: NSViewController, NSWindowDelegate {
override func viewWillAppear() {
super.viewWillAppear()
view.window?.delegate = self
}
func windowWillClose(_ aNotification: Notification) {
print("windowWillClose")
}
}

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

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.

OS X design decisions. Terminate the app on last window close?

Per Apple's Human Interface Guidelines (a guide for Mac developers):

In most cases, applications that are
not document-based should quit when
the main window is closed. For
Example, System Preferences quits if
the user closes the window. If an
application continues to perform some
function when the main window is
closed, however, it may be appropriate
to leave it running when the main
window is closed. For example, iTunes
continues to play when the user closes
the main window.



Related Topics



Leave a reply



Submit