By macOS 10.10, this is somewhat solved by the call to NSViewController
's viewWillAppear
or viewDidAppear
. Have an NSViewController
subclass and set it as the contentViewController
of the window. Then its viewWillAppear
/ viewDidAppear
implementation can post a notification that the window will (or did) open.
How to get notified when NSWindow opens?
-
30-08-2022 - |
Question
How to take notice when an NSWindow
is about to be opened or have just opened? That is, the opposite of windowWillClose:
delegate method (likewise the opposite of NSWindowWillCloseNotification.
)
This is related to this question, but from the other direction.
The background is, I'm looking to couple a window with a tickmark on the main menu (among other things). When the window is shown, the corresponding ̨ menu item should be checked and vice-versa.
Solution 2
OTHER TIPS
It should not normally be a mystery when or how a window is made visible. It should only happen in response to something that your own code is doing. If the window is in a NIB and is marked Visible At Launch, then it shows when your code loads that NIB. Otherwise, it should only show if you call one of the -order...
methods other than -orderOut:
(e.g. -orderFront:
) or -makeKeyAndOrderFront:
. If the window is controlled by a window controller, then it would show if you invoke -[NSWindowController showWindow:]
. Etc.
If you really feel the need to be notified, you can subclass NSWindow
and override -orderWindow:relativeTo:
and, if orderingMode
is not NSWindowOut
and the window was not already visible, post a notification of your own.
You can bind your NSMenuItem
value to the NSWindow
s visible
binding Zero lines of code if you do it in IB.
visible: A Boolean value that specifies if the NSWindow is visible.
If visible evaluates to YES, the NSWindow is visible.
Availability: Available in OS X v10.3 and later.
See the NSWindow Binding Documentation for more info.
You can either bind the NSMenuItem
value binding to an existing NSWindow
property on one of your existing classes, or add an NSObjectController
to your nib and set its content to the NSWindow
instance then bind to the controller.
Tested and confirmed on Mac OS 10.9. Works for window minimization and restoration too.