Я пытаюсь использовать 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)