Peter Lapisu
Peter Lapisu

Reputation: 20975

NSUndoManager Redo prepareWithInvocationTarget

How can i set up a specific Redo action the same i setup Undo actions when using prepareWithInvocationTarget

using my approach, the redo doesn't work (undo works)

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

}

Upvotes: 0

Views: 632

Answers (1)

Peter Lapisu
Peter Lapisu

Reputation: 20975

In the undo method, if called from undoing you should register a 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];

}

Upvotes: 2

Related Questions