NSUndoManager重做prepareWithInvocationTarget

时间:2013-11-20 18:17:58

标签: cocoa nsundomanager redo

如何设置特定的重做操作与使用prepareWithInvocationTarget

时设置的撤消操作相同

使用我的方法,重做不起作用(撤消工作)

- (void)removeSmth:(Smth *)smth index:(NSInteger)indexOfSmth {

    [self.document.undoManager beginUndoGrouping];

    ...

    [self removeSmth:smth];
    [[self.document.undoManager prepareWithInvocationTarget:self] undoInsertSmth:smth index:indexOfSmth];

    ...

    [self.document.undoManager endUndoGrouping];

}

- (void)undoInsertSmth:(Smth *)smth index:(NSUInteger)index {

    [self insertSmth:smth index:index];

}

1 个答案:

答案 0 :(得分:1)

在undo方法中,如果从撤消调用,则应注册撤消

- (void)removeSmth:(Smth *)smth index:(NSInteger)indexOfSmth {

    [self.document.undoManager beginUndoGrouping];

    ...

    [self removeSmth:smth];
    [[self.document.undoManager prepareWithInvocationTarget:self] undoInsertSmth:smth index:indexOfSmth];

    ...

    [self.document.undoManager endUndoGrouping];

}

- (void)undoInsertSmth:(Smth *)smth index:(NSUInteger)index {
    if ([self.document.undoManager isUndoing]) {
        [[self.document.undoManager prepareWithInvocationTarget:self] removeSmth:smth index:index];
    }
    [self insertSmth:smth index:index];

}