How to hide NSMenuItem?

I am currently writing a Mac application in Objective-C and have not been able to figure out in my entire life how to hide NSMenuItem. (Note: yes, I really mean hide, not disable / gray out. I understand the implications of UX for this, but the functionality is actually not what you think. Just tell me about it.)

The documentation still doesn't mention, is this possible?

+4
source share
3 answers

If you defined your NSMenuItem in your header and linked it through your NIB, you can simply call the Hidden property.

 [myMenuItem setHidden:YES]; 

"Drying" menuName will be [myMenuItem setEnabled: NO];

+9
source

The Obj-C property is called "hidden." This means that the basic logic element is called _hidden, and for you 3 accessors are automatically synthesized: 2 isHidden : isHidden and hidden plus one setter: setHidden .

In Obj-C, using dot notation, you can set a property only with:

 myMenuItem.hidden = YES; // or NO 

or in a regular message:

 [myMenuItem setHidden:YES]; // or NO 

to get the value you can: myMenuItem.hidden , myMenuItem.isHidden , [myMenuItem hidden] or [myMenuItem setHidden]

Now Swift is borrowing its naming convention from (in my opinion, lingua inferior) C and C ++. The Boolean property will have both its own setter and getter with the name "isHidden".

When Xcode converts Cocoa Obj-C Framework headers with an Obj-C interface defining a hidden property, it synthesizes the "isHidden" swift property, which is read / write.

This is why you can use both getter and setter:

 if myMenuItem.isHidden { } 

and

 myMenuItem.isHidden = true // or false 

Hope this addresses the issue.

0
source

I suppose the function may have changed to

 [menuItem isHidden: YES] 

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

-1
source

All Articles