Xcode Swift OS X popover behavior

本秂侑毒 提交于 2019-12-04 21:09:51

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()
            })

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 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?

Update for Swift 3

var detector: Any?

detector = NSEvent.addGlobalMonitorForEvents(matching:[NSEventMask.leftMouseDown, NSEventMask.rightMouseDown], handler: { [weak self] event in
        self?.hidingFunction()
    })

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

Update for Swift 4+

var detector: AnyObject?

// make sure you add this just before you open the popup.
detector = NSEvent.addGlobalMonitorForEvents(matching:[NSEvent.EventTypeMask.leftMouseDown, NSEvent.EventTypeMask.rightMouseDown], handler: { [weak self] event in
                    self?.hidingFunction()
                }) as AnyObject



func hidingFunction(){
        popover.performClose(nil)
        if let temp: AnyObject = detector { // using if let to be sure it was intialized
            NSEvent.removeMonitor(temp)
        }
    }
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!