我有一个问题创建一个NSMenuItem
动作,我创建了函数,它为我创建菜单,而在NSMenuItem
与选择器上使用init,我描述的函数不适用于swift 4,如果我使用NSApp.terminate
- NSMenuItem
看到选择器正常工作( )...这是代码:
mainMenuController: NSObject {
func setUpMenus(){
//...
let firstItemMainMenu = NSMenuItem.init(title: "Test", action: #selector(test), keyEquivalent: "") // This one doesn't work
let fourthItemMainMenu = NSMenuItem.init(title: "Quit", action: #selector(NSApp.terminate), keyEquivalent: "q") //This one NSApp.terminate - works
//...
}
这是功能:
@objc func test(_ sender: Any?){
let alert = NSAlert()
alert.addButton(withTitle: "test")
alert.messageText = "test"
alert.runModal()
}
}
有任何想法吗?
两个建议:
Selector("terminate:")
将选择器交给First Responder
(您将收到使用#selector
语法的警告)@objc func quit
,它调用NSApp.terminate