如何隐藏 NSMenuItem?
我目前正在用 Objective-C 编写一个 Mac 应用程序,但我一生都无法弄清楚如何隐藏 NSMenuItem。 (注意:是的,我的意思是隐藏,而不是禁用/灰显。我意识到这样做对用户体验的影响,但功能并不像您想象的那样。请相信我。)
文档没有无论如何提到这样做,有可能吗?
I'm currently writing a Mac App in Objective-C and can't for the life of me figure out how to hide a NSMenuItem. (Note: Yes I really mean hide, not disable/grey-out. I realize the UX implications of doing so, but the functionality isn't really what you think it is. Just trust me on this.)
The documentation doesn't mention anyway to do so, is it even possible?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
如果您已在标头中定义了
NSMenuItem
并通过 NIB 连接它,则只需调用 Hidden 属性即可。“变灰”菜单项将是
[myMenuItem setEnabled: NO];
If you have defined your
NSMenuItem
in your header and connected it through your NIB, you can simply call the Hidden property."Greying out" the menuItem would be
[myMenuItem setEnabled: NO];
我相信该功能可能已更改为
https://developer.apple.com/documentation/appkit/ nsmenuitem
I believe the function may have changed to
https://developer.apple.com/documentation/appkit/nsmenuitem
Obj-C 属性被命名为“隐藏”。这意味着,底层布尔成员名为 _hidden,并且会自动为您合成 3 个访问器:2 个 getter:
isHidden
和hidden
加上 1 个 setter:setHidden
代码>.在 Obj-C 中,使用点表示法,您只能使用以下方式设置属性:
或在普通消息中:
要获取值,您可以
myMenuItem.hidden
、myMenuItem.isHidden
、[myMenuItem hide]
或[myMenuItem setHidden]
现在 Swift 从(我认为语言上较差的)C 和 C++ 借用了其命名约定。布尔属性的 setter 和 getter 都名为“isHidden”。
当 Xcode 使用定义隐藏属性的 Obj-C 接口转换 Cocoa Obj-C Framework 标头时,它会合成一个读/写的“isHidden”swift 属性。
这就是为什么你可以同时使用 getter 和 setter:
并
希望这能解决这个问题
The Obj-C property is named "hidden". This means, the underlying boolean member is named _hidden, and 3 accessors are automatically synthesized for you: 2 getters:
isHidden
andhidden
plus one setter:setHidden
.In Obj-C, using dot notation you can only set the property using:
or in normal message:
to get the value you can either
myMenuItem.hidden
,myMenuItem.isHidden
,[myMenuItem hidden]
or[myMenuItem setHidden]
Now Swift borrows its naming convention from the (lingually inferior in my opinion) C and C++. A boolean property will have both its setter and getter named "isHidden".
When Xcode converts the Cocoa Obj-C Framework headers with the Obj-C interface defining the property hidden --- it synthesizes an "isHidden" swift property which is read/write.
That's why you can use both as getter and setter:
and
Hope this covers the issue