[英]NSMennuItems used in menubar right click menu appear grayed out and cant be clicked
我正在嘗試使用NSTextField
作為NSMenuItem
。
但它不起作用。
這是我的代碼:
class charPoolBoxForMenuItemInstance: NSTextField,NSTextFieldDelegate{
override init(frame: CGRect) {
super.init(frame: frame)
self.delegate = self
}
required init?(coder: NSCoder) {super.init(coder: NSCoder.init())}
override func textDidChange(_ notification: Notification) {
if self.currentEditor()?.selectedRange != nil{UserDefaults.standard.set(self.stringValue, forKey: "charPoolBox")
}
}
}
var charPoolBoxForMenuItem = charPoolBoxForMenuItemInstance.init(string: String.init())
然后我將它添加到我的菜單中,如下所示:
let charPoolMenuItem = NSMenuItem()
charPoolMenuItem.title = "Character Pool"
charPoolMenuItem.view = charPoolBoxForMenuItem
charPoolBoxForMenuItem.placeholderString = "Character Pool"
charPoolBoxForMenuItem.frame = CGRect(x: 30, y: 0, width: 400, height: 22)
menuBarMenu.addItem(charPoolMenuItem)
這是它變灰的樣子:
更新 :
@objc func resetPoolChar(){charPoolBoxForMenuItem.stringValue = "abc"}
let charPoolMenuItem = NSMenuItem()
charPoolMenuItem.title = "Character Pool"
charPoolMenuItem.view = charPoolBoxForMenuItem
charPoolMenuItem.action = #selector(resetPoolChar)
charPoolBoxForMenuItem.placeholderString = "Character Pool"
charPoolBoxForMenuItem.frame = CGRect(x: 30, y: 0, width: 400, height: 22)
您還需要操作的目標:
charPoolMenuItem.action = #selector(resetPoolChar)
charPoolMenuItem.target = self
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.