Cocoa NSUndoManager重做准备InvocationTarget

Cocoa NSUndoManager重做准备InvocationTarget,cocoa,nsundomanager,redo,Cocoa,Nsundomanager,Redo,使用prepareWithInvocationTarget 使用我的方法,重做不起作用(撤消起作用) 在undo方法中,如果从undo调用,则应注册一个undo - (void)removeSmth:(Smth *)smth index:(NSInteger)indexOfSmth { [self.document.undoManager beginUndoGrouping]; ... [self removeSmth:smth]; [[self.docum

使用
prepareWithInvocationTarget

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


在undo方法中,如果从undo调用,则应注册一个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];

}
- (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];

}