Home

windowWillClose

windowWillClose is a method of the NSWindowDelegate protocol in macOS AppKit. It is invoked when a window is about to close, typically after the windowShouldClose: method has indicated that closing should proceed. The method receives an NSNotification whose object is the NSWindow that will close, allowing the delegate to respond to the impending closure.

In Objective-C the signature is:

- (void)windowWillClose:(NSNotification *)notification;

In Swift it is:

func windowWillClose(_ notification: Notification)

Usage focuses on cleanup and state handling prior to the window disappearing. Implementations commonly release resources,

Relationship to related methods:

- windowShouldClose: is asked first to determine whether the window should close; if it returns YES (true),

- windowWillClose: is then called before the window is actually closed.

- After closure, windowDidClose: may be sent to signal that the window has finished closing.

There is also a notification, NSWindowWillCloseNotification, which posts the same event to NotificationCenter and can be

Notes:

- If your app quits when the last window closes, you may handle termination logic in applicationShouldTerminateAfterLastWindowClosed

- Observing NSWindowWillCloseNotification provides an alternative to implementing the delegate method.

Example use cases include saving unsaved work, releasing resources tied to the window, or updating the

save
user
data,
or
update
application
state.
The
window
instance
can
be
retrieved
from
notification.object
(Objective-C)
or
notification.object
as?
NSWindow
(Swift).
closing
proceeds.
observed
if
a
delegate
is
not
used.
or
windowDidClose.
user
interface
to
reflect
that
the
window
is
about
to
disappear.