swift - 菜单栏右键菜单中使用的NSMenuItems显示为灰色无法点击
问题描述
我正在尝试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
推荐阅读
- javascript - 如何通过麦克风通过语音为我网页上的用户自动填写表单?
- python - 如何获取 pg.infiniteLine 已在 pyqtgraph 中移动的事件
- wordpress - Wordpress Ultimate Member:不处于编辑模式时无法查看页面上的所有自定义字段
- python - 如何在多个位置或功能访问从列表中选择的项目
- javascript - 如何让 http-server(npm 包)接受 ES6 模块语法?
- ios - `setCollectionViewLayout` 动画在更改集合视图框架时中断
- c - Fseek 回滚 8 个字节,但我希望它是 4 个字节
- ios - iOS 设备是否将 GPS 位置存储在其 .mov 文件中
- react-native - 如何在 React Native Paper 中更改 list.accordion 上的rippleColor 效果的颜色
- woocommerce - 修改后的图像 WooCommerce 缺货消息仅适用于非托管库存