我試圖檢測字串何時被拖到 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)
被呼叫。為什么我的代表沒有被召喚的任何建議。
謝謝禮
薩
uj5u.com熱心網友回復:
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
}
}
轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/497955.html
標籤:迅速 苹果系统 可可 nsstatusitem