菜单栏右键菜单中使用的NSMennuItems显示为灰色,无法单击

时间:2019-04-30 08:17:46

标签: swift macos cocoa

我正在尝试将;用作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())

这是灰色的样子:

enter image description here

更新:

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)

1 个答案:

答案 0 :(得分:0)

您还需要操作的目标:

   charPoolMenuItem.action = #selector(resetPoolChar)
   charPoolMenuItem.target = self