关于:NSMenuDelegate methods not called for contextual menu一旦 View 可见,我的 View 委托(delegate)就不会被调用。
我想根据按键修饰符动态切换newWindowMenuItem
的标题。
func menuWillOpen(_ menu: NSMenu) {
guard newWindowMenuItem.title != "close" else {
return
}
newWindowMenuItem.title = (appDelegate.shiftKeyDown) ? "Tab" : "Window"
menu.update()
}
func menuDidClose(_ menu: NSMenu) {
if menu == newWindowMenuItem.menu {
newWindowMenuItem.title = "closed"
}
}
func menu(_ menu: NSMenu, willHighlight item: NSMenuItem?) {
if let currentEvent = NSApp.currentEvent, item == newWindowMenuItem {
let flags = currentEvent.modifierFlags
newWindowMenuItem.title = (flags.contains(.command)) ? "Tab" : "Window"
menu.update()
Swift.print("title \(newWindowMenuItem.title)")
}
}
在 viewDidLoad() 中注册为
NotificationCenter.default.addObserver(
self,
selector: #selector(WebViewController.shiftKeyDown(_:)),
name: NSNotification.Name(rawValue: "shiftKeyDown"),
object: nil)
我试图允许用户在按下修饰符时动态更改菜单项 - 主要是标题。由于这种情况发生在绘制菜单之后,因此使用 menuWillOpen() 似乎不可行,否则他们可以在绘制之前按下修饰符 - 除了 CTRL 之外。
我正在考虑完全跳过委托(delegate) - 因为我没有阅读适合我的需要的键修饰符更改的方法,而是让一个键监视器影响更改:
internal func shiftKeyDown(_ notification : Notification) {
let shiftKeyDown : NSNumber = notification.object as! NSNumber
Swift.print(String(format: "shift %@", shiftKeyDown.boolValue ? "v" : "^"))
guard webView.menu != nil else {
return
}
newWindowMenuItem.title = shiftKeyDown.boolValue ? "Tab" : "Window"
newWindowMenuItem.menu?.update()
}
但这从未被调用或关键事件,后菜单显示表明我的方法是错误的?
最佳答案
您没有正确注册通知处理程序。方法
internal func shiftKeyDown(_ notification : Notification)
没有机会被调用,因为它没有被标记为 @objc
。应该这么写
@objc internal func shiftKeyDown(_ notification : Notification)
您可能还有另一个shiftKeyDown
某个地方确实被调用的方法,或者您的注册代码根本不起作用。 (或者你在某处有一个类似的命名方法,但在某处有一个拼写错误。)
( NotificationCenter
仍然只是 Objective C NSNotificationCenter
的一个花哨的 Swift 名称,而它又基于更旧的 CFNotificationCenter。而 Swift 的整个 #selector
功能是构建在 objc 之上的-runtime,因此它仅适用于 objc-methods。)
关于swift - 上下文菜单期间未调用键修饰符监视器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52467996/