Actions Assigned to Nsmenuitem Dont Seem to Work

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,

  1. Action
    menuItem.action: #selector(YOURFUNCTION)

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



Leave a reply



Submit