11

Is there a way to check if a NSWindow is visible or not? I want to display a sheet controller once the first window of my app became visible (the animation on 10.7 ended and the user can see the window!). If I just show the sheet in windowDidLoad, it results in a stupid looking animation (sheet rolling out, window popping out from the back). I know that NSWindowDelegate provides two methods which are invoked when a window either became the key window or the main window, however, this doesn't have to mean that the window is already fully visible at the time. This is even more noticeable on Lion where windows tend to pop up with this stupid animation.

JustSid
  • 25,168
  • 7
  • 79
  • 97
  • I thought I had read about some notification when the window finished animating on screen, but now I can't find it... – jtbandes Aug 16 '11 at 17:19

2 Answers2

30

I would go for something like this:

if ([myWindow isVisible]) {
    // Do stuff
}

Or an an observer for this key path to be notified when the change occurs.

PeyloW
  • 36,742
  • 12
  • 80
  • 99
  • I guess its time for me to sleep a bit, I searched the NSWindow documentation for like 30 minutes looking for such a function and was 100% sure that something like this didn't exist. Thanks a lot! – JustSid Aug 16 '11 at 17:45
  • 3
    @JustSid - I know the feeling, grinding on a problem seldom helps. A break and fresh look next day often do :). – PeyloW Aug 16 '11 at 19:24
2

For what it's worth, you can also bind to the window.visible property. Xcode 4 may squawk at you, saying it's not a bindable property, but it will work.

This can be useful if you are trying enable/disable show/hide NSStatusItem based on whether the window is visible, as well as other approaches.

i.e. in Interface Builder:

  • Bind to: App Delegate
  • Model Key Path: self.window.visible
Scott Allen
  • 1,199
  • 1
  • 12
  • 14