当失去焦点时,以编程方式关闭NSWindow [英] Programmatically closing an NSWindow when it loses focus
问题描述
我正在制作一个图像选择器,当选择器弹出时,将显示一个可选择按钮的n乘n网格。这个网格的按钮将包含在NSWindow中,但是如果用户点击屏幕,我想要窗口自动关闭。有一个标志,可以设置,以便当窗口失去焦点时,它会自动关闭?
I am making an image picker that will display an n by n grid of selectable button when the picker is popped up. This grid of buttons will be contained within an NSWindow but I would like for the window to be close automatically if the user clicks off the screen. Is there a flag that can be set so that when the window looses focus it will be closed automatically?
推荐答案
有两种您可能感兴趣的通知: NSWindowDidResignKeyNotification
和 NSWindowDidResignMainNotification
。你可以简单地注册一个你感兴趣的awakeFromNib(或windowDidLoad如果你有一个自定义控制器),然后在收到通知时适当地关闭或隐藏窗口。
There are two notifications that you may be interested in: NSWindowDidResignKeyNotification
and NSWindowDidResignMainNotification
. You can simply register for the one you're interested in in awakeFromNib (or windowDidLoad if you have a custom controller) and then close or hide the window as appropriate when you receive the notifications.
我不会深入研究这是否从UI的角度来看是一个好主意。但是,对于您描述的功能,您可以选择重叠式视图或面板。
I won't delve too much into whether or not this is a good idea from UI standpoint. But, it might be a better idea to have either an overlay view or a panel for the functionality you describe.
这篇关于当失去焦点时,以编程方式关闭NSWindow的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!