macos 如何检查 NSWindow 是否可见

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/7081957/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-21 08:18:44  来源:igfitidea点击:

How to check if a NSWindow is visible

objective-ccocoamacosnswindow

提问by JustSid

Is there a way to check if a NSWindowis 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 NSWindowDelegateprovides 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.

有没有办法检查 aNSWindow是否可见?一旦我的应用程序的第一个窗口可见(10.7 上的动画结束并且用户可以看到该窗口!),我想显示一个工作表控制器。如果我只是在 中显示工作表windowDidLoad,它会产生一个看起来很愚蠢的动画(工作表滚出,窗口从后面弹出)。我知道它NSWindowDelegate提供了两种方法,当窗口成为关键窗口或主窗口时会调用它们,但是,这并不一定意味着该窗口当时已经完全可见。这在 Lion 上更加明显,因为这个愚蠢的动画往往会弹出窗口。

回答by PeyloW

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.

或者在发生更改时通知此关键路径的观察者。

回答by Scott Allen

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.

对于它的价值,您还可以绑定到 window.visible 属性。Xcode 4 可能会对你大吼大叫,说它不是一个可绑定的属性,但它会起作用。

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.

如果您尝试根据窗口是否可见以及其他方法启用/禁用显示/隐藏 NSStatusItem,这会很有用。

i.e. in Interface Builder:

即在界面生成器中:

  • Bind to: App Delegate
  • Model Key Path: self.window.visible
  • 绑定到:App Delegate
  • 模型键路径:self.window.visible