Xcode Swift OS X弹出行为 [英] Xcode Swift OS X popover behavior

查看:122
本文介绍了Xcode Swift OS X弹出行为的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

对于我的小型Mac菜单栏应用程序,我希望弹出窗口的行为是暂时的,因此当失去焦点时,它将关闭.这适用于:

For my small Mac menubar application I'd like the behavior of the popover to be transient, so when it loses focus, it will close. This works for that:

popover.behavior = NSPopoverBehavior.Transient

但是它只能工作一次,因此第二次单击其他位置时,弹出框保持不变.我将代码放置在func applicationDidFinishLaunching(notification: NSNotification)中,但是将其放置在类中此函数之外的地方无效.我怎么能一直强迫这种行为?

But it only works once, so the second time you click somewhere else the popover stays. I placed the code in func applicationDidFinishLaunching(notification: NSNotification), but placing it outside this function inside the class did not work. How can I use force this behavior all the time?

我正在将Xcode 7.0与Swift(2.0)结合使用.

I am using Xcode 7.0 with Swift (2.0).

推荐答案

最好将行为保留为NSPopoverBehaviorApplicationDefined的默认值,并实现必要的功能来处理它.两种行为尚不清楚.您可以执行以下操作:

You better leave the behaviour to the default value which is NSPopoverBehaviorApplicationDefined and you implement necessary function to handle it.Because as it says in Apple Documentation the circumstances of the other two behaviours are not clear. You can do as follows:

detector = NSEvent.addGlobalMonitorForEventsMatchingMask([NSEventMask.LeftMouseDownMask, NSEventMask.RightMouseDownMask], handler: { [weak self] event in
                self?.hidingFunction()
            })

这会在执行左/右键单击时向全局事件注册一个监控器
现在,在将上述处理程序指定为self的同一类中,实现hideFunction().
此功能将关闭弹出窗口并删除创建的监视器

this registers a montior to a global event when left/right click is performed
Now you implement the hidingFunction() in the same class you made the above call in as the handler was specified as self.
This function will close the popover and and remove the monitor created

func hidingFunction(){
 popover.close()
 if let temp: AnyObject = detector { // using if let to be sure it was intialized
    NSEvent.removeMonitor(temp)
} 

detector只是一个变量名,您可以在类的顶部将其定义为任何对象的类型,然后随意命名它

detector is just a variable name you can name it whatever you want define it before at the top of the class as type of any object

var detector: AnyObject?

这篇关于Xcode Swift OS X弹出行为的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆