validateMenuItem: not called
Here is the solution :
Cocoa looks for the validateMenuItem:
method in the Class where the NSMenuItem
's action selector is.
So, if your NSMenuItem
's action selector (e.g. @selector(someSelector:)
) is implemented in SomeClass
, then make sure you have a validateMenuItem:
method in SomeClass
too, if you want to validate the corresponding menu items.
@Dr.Kameleon has the right answer.
I'll add one small point to update it if that's OK? My code broke recently in this area and stopped calling validateMenuItem: when it was working before. I didn't notice because the only symptom was a menu item no longer disabled when it should.
The issue was Swift 4. The method must be attributed with @objc. In earlier versions of Swift, inheriting from NSObject, NSViewController, etc. was enough but that changed with the newer versions of Swift, 4 and 5.
p.s. It seems it is fine to put the method in an extension.