简体   繁体   English

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

[英]NSMennuItems used in menubar right click menu appear grayed out and cant be clicked

I am trying to use NSTextField as a NSMenuItem .我正在尝试使用NSTextField作为NSMenuItem

But it's not working.但它不起作用。

Here is my code:这是我的代码:

 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())

Then I add it to my menu like so:然后我将它添加到我的菜单中,如下所示:

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)

And here's how it looks greyed out:这是它变灰的样子:

在此处输入图片说明

update :更新 :

@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)

You also need the target for the action:您还需要操作的目标:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM