It is difficult for me to execute NSUndoManager, I tried to read the documentation on it, but I can not figure it out. This is what I have tried so far. I created an application that draws strings by connecting two points in an array, I applied the cancellation method by deleting the last object, but I canโt figure out how to implement repeat, I came across NSUndoManager and started reading its documentation, but I donโt know how to apply it to my problem. Here is the code that I have at the moment
-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event { NSUInteger taps = [[touches anyObject]tapCount]; if(taps == 2) { [self setNeedsDisplay]; } else { if([self.pointsArray count] == 0) { self.pointsArray = [[NSMutableArray alloc]init]; UITouch *t = [touches anyObject]; CGPoint startLoc = [t locationInView:self]; [self.pointsArray addObject:[NSValue valueWithCGPoint:startLoc]]; } } } -(void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event { UITouch *t = [touches anyObject]; CGPoint currentLoc = [t locationInView:self]; [self.pointsArray addObject:[NSValue valueWithCGPoint:currentLoc]]; [self setNeedsDisplay]; } #pragma mark - Undo/Redo Methods -(void)undo:(id) object { [[undoManager prepareWithInvocationTarget:self]redo:object]; [undoManager setActionName:@"undoLineSegment"]; [self.pointsArray removeLastObject]; } -(void)redo:(id)object { [self.pointsArray addObject:object]; [[undoManager prepareWithInvocationTarget:self]undo:object]; [undoManager setActionName:@"RedoUndoneLineSegment"]; } - (IBAction)undoButton:(UIButton *)sender { [self.undoManager undo]; [self setNeedsDisplay]; } - (IBAction)redoButton:(UIButton *)sender { [self.undoManager redo]; [self setNeedsDisplay]; }
I get no errors, but at runtime, when I click the buttons, nothing happens. What I don't understand about NSUndoManager is what happens, what the "object" is. Don't I declare what I need to declare.
Thanks,
source share