开发者

How can I ensure that Undo and Redo menu items have the action string added?

开发者 https://www.devze.com 2023-02-18 02:37 出处:网络
I have undo/redo working in my app. I set the target of the undo and redo menu actions to my app delegate to implement the actions with undoAction: and redoAction: methods. The menu items\' enabled b

I have undo/redo working in my app.

I set the target of the undo and redo menu actions to my app delegate to implement the actions with undoAction: and redoAction: methods. The menu items' enabled bindings are bound to canUndo and canRedo properties of my undo manager and these work as expected. I set an action name when I push to the undo stack, but never see it in the menu.

Why does my menu always say Undo or Redo, not Undo Move to Trash and Redo Move to Trash ? Does the menu item normally get updated by some magic I am bypassing? I have not used the responder chain because my app delegate is not in the responder chain, and so never sees the actions.

The undo manager is created in my app delegate.

Update: changing to use the window's undo manager makes no difference.

-(void)undoableRemoveItemsFromListAtIndexes:(NSIndexSet*)itemIndexes;
{
    NSArray *removedItems = [[self.listController arrangedObjects] objectsAtIndexes:itemIndexes];
    [[self.undoManager prepareWithInvocationTarget:self] undoableAddItems:removedItems toListAtIndexes:itemIndexes];
    [self removeItemsFromListAtIndexes:itemIndexes];
    [开发者_开发百科self.undoManager setActionName:@"Move to Trash"];
}


Check to see if the undo manager -isUndoing and swap the strings appropriately.

NSUndoManager * um = [self undoManager];
// ...
[um setActionName:([um isUndoing]) ? @"Remove Foo" : @"Add Foo"];

It's important to remember a redo is an undo pushed to the redo stack.


The answer turned out to be one or more of the following (from help at NSCoder night Campbell):

  • Use the window's undo manager, not one that I created

  • Remove the bindings I had and hook up the menu items to First Responder

  • Name the methods in my app delegate undo: and redo:

Once I did all that it all magically worked.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号