Re: validateMenuItem Not Called for Undo: Menu Item?
Re: validateMenuItem Not Called for Undo: Menu Item?
- Subject: Re: validateMenuItem Not Called for Undo: Menu Item?
- From: Quincey Morris <email@hidden>
- Date: Thu, 25 Aug 2011 19:00:14 -0700
On Aug 25, 2011, at 18:17 , Vik Rubenfeld wrote:
> Going through these requirements, it appears my app is fulfulling all of them. My undo menu item has its Sent Action set to undo: in First Responder. First Responder at the time of the anomaly is an NSDocument called myQuestionnaireEditor. This can be verified because the Cut and Paste menu items also have their Sent Actions set to First Responder, and validateMenuItem method in myQuestionnaireEditor is called for them as expected.
>
> myQuestionnaireEditor does have a method for implementing undo:
>
> -(IBAction)undo:(id) sender {
> [[self undoManager] undo];
> }
>
> myQuestionnaireEditor does implement validateMenuItem:
>
> - (BOOL)validateMenuItem:(NSMenuItem *)menuItem {
>
> NSLog(@"validateMenuItem - %@", [menuItem title]);
> BOOL returnValue = NO;
>
> if ([menuItem action] == @selector(undo:)) {
> return [[self undoManager] canUndo];
> }
>
> {......}
> return [super validateMenuItem:menuItem];
> }
Configuring the Undo menu item with action 'undo:' tells the frameworks that this is *the* undo menu item, so it becomes one element of a standard undo UI mechanism implemented in NSWindow. Under these circumstances, the menu item (and especially its validation) isn't yours to mess with. The proper way to provide an undo manager object to this mechanism is to use the 'windowWillReturnUndoManager:' window delegate method.
OTOH, if you want to take implement the entire mechanism yourself, then you should use a custom action such as 'myUndo:' for the Undo menu item. That means, as far as the frameworks know, you app doesn't have (standard) undo, and the action/validation should work in the normal way.
If the above code really is your implementation, then the former technique is probably the correct one. (It will, for example, allow you to set Undo action names normally, so you won't have to modify the menu item times directly yourself). If your code needs to do something more complicated than you're showing here, then use the latter technique, and you're on your own for implementing every detail of the UI.
_______________________________________________
Cocoa-dev mailing list (email@hidden)
Please do not post admin requests or moderator comments to the list.
Contact the moderators at cocoa-dev-admins(at)lists.apple.com
Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden