test
test

Reputation: 123

ValidateMenuItem is not getting called for NSMenuItem

Here is my sample class and usage:

@interface CCocoaMenuItem : NSMenuItem 
{
   someClass *someobj;
}

 - (void)menuEventHandler:(id)target;
- (void)setEnableItem:(BOOL)nEnabled;

@end

@implementation CCocoaMenuItem

- (BOOL)validateMenuItem:(NSMenuItem *)item {
    // return YES or NO based on some conditions;
// But this method is not getting called


}

@end
 CCocoaMenuItem *dummyItem = [[CCocoaMenuItem allocWithZone:[NSMenu menuZone]] initWithTitle:(NSString*)aStr action:nil keyEquivalent:@""] autorelease];
[dummyItem setAction:@selector(menuEventHandler:)];
[dummyItem setTarget:dummyItem];

here validateMenuItem is not getting called. I have set the action and target. Target is this class object itself and I have defined validatemenuItem in this class only.

Is there anything I am missing here?

Upvotes: 0

Views: 1904

Answers (2)

ozmax
ozmax

Reputation: 470

In order to call validateMenuItem:, because it is a delegate method of NSMenuDelegate,
you have to do something like this:

@interface CCocoaMenuItem : NSMenuItem <NSMenuDelegate>{
   someClass *someobj;
}

Upvotes: 0

omz
omz

Reputation: 53561

In the code you've posted, CCocoaMenuItem only declares the menuEventHandler: method in the @interface, but doesn't actually implement it. Menu items aren't validated if their target doesn't respond to the selector you've set as the action (such menu items are disabled automatically).

Btw, menuZone is meant for NSMenu, not NSMenuItem.

Upvotes: 1

Related Questions