我试图检测一个字符串何时被拖到NSStatusItem上。下面是我的代码在AppDelegate中的实现:
func applicationDidFinishLaunching(_ aNotification: Notification) {
if let button = statusItem.button {
button.image = NSImage(named:NSImage.Name("StatusBarButtonImage"))
button.action = #selector(menubarAction(_:))
button.sendAction(on: [.leftMouseUp, .rightMouseUp])
button.window?.registerForDraggedTypes([.string ])
button.window?.delegate = self
}
}
这是我的NSWindow委托调用
extension AppDelegate:NSWindowDelegate{
func draggingEntered(_ sender: NSDraggingInfo) -> NSDragOperation{
NSApplication.shared.activate(ignoringOtherApps: true )
return .copy
}
func performDragOperation(_ sender: NSDraggingInfo) -> Bool{
NSApplication.shared.activate(ignoringOtherApps: true )
return true
}
}
然而,当一个字符串被拖到NSStatusItem上时,这些委托不会被调用。我知道拖拽被检测为:
applicationDidBecomeActive(_ notification: Notification)
被调用。为什么我的委托没有被调用。
谢谢
Reza
draggingEntered
和performDragOperation
为协议NSDraggingDestination
的方法。如果类不采用协议,Swift不会调用协议方法。AppDelegate
必须采用NSDraggingDestination
extension AppDelegate: NSWindowDelegate {
}
extension AppDelegate: NSDraggingDestination {
func draggingEntered(_ sender: NSDraggingInfo) -> NSDragOperation{
NSApplication.shared.activate(ignoringOtherApps: true )
return .copy
}
func performDragOperation(_ sender: NSDraggingInfo) -> Bool{
NSApplication.shared.activate(ignoringOtherApps: true )
return true
}
}