Validatemenuitem or Menuwillopen Not Called for Nsmenu

validateMenuItem or menuWillOpen not called for NSMenu

menuWillOpen: belongs to the NSMenuDelegate protocol; for it to be called the menu in question needs a delegate:

let statusBarMenu = NSMenu(title: "")
statusBarMenu.delegate = self

validateMenuItem: belongs to the NSMenuValidation informal protocol; for it to be called the relevant menu items must have a target. The following passage is taken from Apple's Application Menu and Pop-up List Programming Topics documentation:

When you use automatic menu enabling, NSMenu updates the status of every menu item whenever a user event occurs. To update the status of a menu item, NSMenu first determines the target of the item and then determines whether the target implements validateMenuItem: or validateUserInterfaceItem: (in that order).

let myMenuItem = NSMenuItem()
myMenuItem.target = self
myMenuItem.action = #selector(doSomething)

validateMenuItem not called for every menu item

Answer can be found here:
validateMenuItem or menuWillOpen not called for NSMenu

validateMenuItem: belongs to the NSMenuValidation informal protocol;
for it to be called the relevant menu items must have a target.

Display menu on NSTableView if a row is selected

It was pretty easy:

Implement

NSMenuDelegate

and add this method:

func menuWillOpen(_ menu: NSMenu) {
if myTableView.selectedRow < 0 {
menu.cancelTrackingWithoutAnimation()
}
}

Is there a way to assign multiple key equivalents to a Menu Item in Cocoa (via IB or programmatically)?

Make a second one (easiest way being to duplicate it) and set it as hidden. It won't show up when the user pulls open the menu, but as long as it's enabled, its key equivalents should still be in effect.

How do I customise the contextual menu of a PDFView?

Subclass PDFView and override func menu(for event: NSEvent) -> NSMenu?. Call super and adapt the menu.

Adopt protocol NSMenuItemValidation and implement func validateMenuItem(_ menuItem: NSMenuItem) -> Bool to enable and disable the items.



Related Topics



Leave a reply



Submit