简体   繁体   中英

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

[myMenuItem setHidden:YES];

"Greying out" the menuItem would be [myMenuItem setEnabled: NO];

I believe the function may have changed to

[menuItem isHidden: YES]

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

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 and hidden plus one setter: setHidden .

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

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]

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:

if myMenuItem.isHidden {
}

and

myMenuItem.isHidden = true // or false

Hope this covers the issue

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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