views:

19

answers:

1

Im trying to add an NSUndoManager to my program but im not sure how to register the methods with the manager using :

[myUndoManager registerUndoWithTarget:selector:object:];

what if i have the following method:

-(IBAction)tapButton:(id)sender {
   myFoo++;    
   yourFoo++;    //incrementing integers
   fooFoo++;
}

how can i register this method with the undo manager? the object of the selector (sender) is not what i want to register. I need to decrement myFoo, yourFoo, and fooFoo with a single undo call.

+1  A: 

Write another method -(void) decrementIntegers and register that like this:

[undoManager registerUndoWithTarget: self selector: @selector( decrementIntegers ) object: nil];

In this method you need to register your original method again to provide redo:

[undoManager registerUndoWithTarget: self selector: @selector( tapButton: ) object: self];

But a better way to do this would be to use accessors for your integers and do undo registering in there. Something like this:

- (void) setMyFoo: (int) newMyFoo;
{
   if (myFoo != newMyFoo) {
      [[undoManager prepareWithInvocationTarget: self] setMyFoo: myFoo];
      myFoo = newMyFoo;
   }
}
Sven
yep, works like a charm! thnx
RAV