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 theNSMenuValidation
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
Pdf417 Decode and Generate The Same Barcode Using Swift
Type Alias Declaration with Templates in Swift
Sandbox Entitlement to Script Itunes via Nsapplescript
Could Not Find an Overload for '-' That Accepts The Supplied Arguments
Populate a Multidimensional Array with a Loop
Double Variable in Mkmapitem Array
Mapping a Dictionary to an Array of Structs in Swift
Swift Use 'Is' for Function Type, Compiler Behavior Is Different with Runtime
Changing Associated Value of Enum Swift
Return Object for a Method Inside Completion Block
How to Show an Alert in Swift UIalertview Not Working
Cannot Use Mutating Member on Immutable Value of Type 'string'
Trim Video Always Fail When Use Avassetexportpresetpassthrough