Actions assigned to NSMenuItem dont seem to work
menuItem.target = self
You need to set the target to 'self'. NSMenuItems have two basic requirements. An action, and a target,
Action
menuItem.action: #selector(YOURFUNCTION)
Target
menuItem.target = self
So to get your menu items working, replace the for loop (within your init call) with this new one:
for (_, val) in service.list {
let menuItem = menu.addItem(NSMenuItem(title: val, action: #selector(toggleService), keyEquivalent: ""))
menuItem.target = self
}
Where can I declare my trigger for NSMenu?
My answer to this was to use an Storyboard NSMenu from the SwiftUI default Project with an AppDelegate. This works perfect, so I recommend this as an solution for this.
NSMenu and NSStatusItem action wont work together
"If the status item has a menu set, the action is not sent to the target when the status item is clicked; instead, the click causes the menu to appear."- apple dev NSStatusItem.action
NSMenuItem custom view does not respond to keyEquivalent
I know this is an old post and you are probably long past this, but I was having the same problem and encountered your post multiple times when trying to find a solution, so I thought I would share what worked for me.
I was able to work around the problem by subclassing NSApplication and overriding sendEvent. You mentioned you tried this with no success, so I don't know what the difference is, but I had no problem intercepting the events from a NSMenu in a NSStatusBarItem. My key equivalents are all ints, so I validate the keys, find the appropriate item, and then explicitly invoke the NSMenuItem action.
-(void)sendEvent:(NSEvent *)theEvent
{
if([theEvent type] == NSKeyUp){
NSInteger mod = ([theEvent modifierFlags] & NSDeviceIndependentModifierFlagsMask);
if(mod == NSCommandKeyMask) {
NSInteger keyEquiv = [[theEvent characters] isEqualToString:@"0"]
? 10
: [[theEvent characters] integerValue];
if(keyEquiv > 0) {
NSMenuItem *item = [[(MyAppDelegate *)[self delegate] myStatusMenu] itemAtIndex:(keyEquiv - 1)];
if([[item keyEquivalent] integerValue] == keyEquiv){
[[item target] performSelector:[item action] withObject:item];
}
}
}
}
[super sendEvent:theEvent];
}
NSMenuItem setHidden doesnt work?
NSMenuItem
hide problems are due to alternate items. If item have alternate item or items it can't be hidden. But You can solve it like this:
For example You want to hide item2 with alternate items itemAlt2 and itemCtrl2. So make itemAlt2 and itemCtrl2 not alternates and hidden like this:
[itemAlt2 setAlternate: NO];
[itemAlt2 setHidden: YES];
[itemCtrl2 setAlternate: NO];
[itemCtrl2 setHidden: YES];
[item2 setHidden: YES];
And when You need visible item2 just make them alternate and visible like this:
[itemAlt2 setAlternate: YES];
[itemAlt2 setHidden: NO];
[itemCtrl2 setAlternate: YES];
[itemCtrl2 setHidden: NO];
[item2 setHidden: NO];
NSMenuItem Key Equivalent not working when menu is hidden
You can put the menu items in both, the application's menubar and the context menu. The key equivalent will then work even without the context menu being visible.
Related Topics
Realitykit - Stored Entities & Load The Scene Aynchronously from The Url
How to Get a Class with Generic Type Accept an Array of Different by Same Generic Types
How to Pass a String Containing Markdown to Custom View
How to Send Multiple Buttons in Button.Addtarget Action? Swift3
How to Refer to a Global Type from Within a Class That Has a Nested Type with The Same Name
Wkwebview Calayer to Image Exports Blank Image
What Do Detached and Assigncurrentcontext Meaning
Swift "Print" Doesn't Appear in Stdout But 3Rd Party C Library Logs Do When Running in Docker on Ecs
Invalid Update: Invalid Number of Rows in Section 1
Rxswift + Mvvm + Coordinator Pattern, How to Wait for Coordinator Result Sequentially
How to Change The Default Skscene That Displays on Startup
Why Is This Predicate Format Being Turned into '= Nil'
How to Loop Over The Output of a Publisher with Combine
Apple Mach-O Linker Error (Static, Not Ld)
How to Provide Default Implementation of an Objective-C Protocol in a Swift Protocol Extension
Swift: Check Which Value in Nsarray Is Closest to Another Given Value