简体   繁体   English

如何隐藏NSMenuItem?

[英]How to hide a 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. 我目前正在Objective-C中编写一个Mac应用程序,并且在我的生活中无法弄清楚如何隐藏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? 文档没有提到这样做,甚至可能吗?

If you have defined your NSMenuItem in your header and connected it through your NIB, you can simply call the Hidden property. 如果您已在标头中定义了NSMenuItem并通过NIB连接它,则只需调用隐藏属性即可。

[myMenuItem setHidden:YES];

"Greying out" the menuItem would be [myMenuItem setEnabled: NO]; “灰色”menuItem将是[myMenuItem setEnabled: NO];

I believe the function may have changed to 我相信这个功能可能已改为

[menuItem isHidden: YES]

https://developer.apple.com/documentation/appkit/nsmenuitem https://developer.apple.com/documentation/appkit/nsmenuitem

The Obj-C property is named "hidden". Obj-C属性被命名为“隐藏”。 This means, the underlying boolean member is named _hidden, and 3 accessors are automatically synthesized for you: 2 getters: isHidden and hidden plus one setter: setHidden . 这意味着,底层布尔成员名为_hidden,并为您自动合成3个访问器:2个getter: isHiddenhidden加上一个setter: setHidden

In Obj-C, using dot notation you can only set the property using: 在Obj-C中,使用点表示法只能使用以下方法设置属性:

myMenuItem.hidden = YES; // or NO

or in normal message: 或者在正常消息中:

[myMenuItem setHidden:YES]; // or NO

to get the value you can either myMenuItem.hidden , myMenuItem.isHidden , [myMenuItem hidden] or [myMenuItem setHidden] 获取值可以是myMenuItem.hiddenmyMenuItem.isHidden[myMenuItem hidden][myMenuItem setHidden]

Now Swift borrows its naming convention from the (lingually inferior in my opinion) C and C++. 现在,斯威夫特从(在我看来,语言上的劣势)C和C ++中借用了它的命名约定。 A boolean property will have both its setter and getter named "isHidden". 布尔属性将其setter和getter命名为“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. 当Xcode使用定义隐藏属性的Obj-C接口转换Cocoa Obj-C Framework头时,它会合成一个“isHidden”swift属性,它是读/写的。

That's why you can use both as getter and setter: 这就是为什么你可以同时使用getter和setter:

if myMenuItem.isHidden {
}

and

myMenuItem.isHidden = true // or false

Hope this covers the issue 希望这能涵盖这个问题

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

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