I am trying to implement NSUndoManagerin my iOS application. I got the undo functionality, but not the reusable part. I'm new to iOS development, and this is the first time I've used it NSUndoManager, so it is probably something trivial.
My application is a drawing / note application, I have a rollback / re-stack with the last ten UIImage(I don't know if this is the most efficient way) in the array. When the user makes changes to the current image, the old image is pushed onto the stack, and the first image in the array is deleted if there are already ten objects in the array. I have an instance variable intthat I use to track objects in an array and make sure the correct image is displayed. My code is as follows:
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
if (oldImagesArrays.count >= 10) {
[oldImagesArrays removeObjectAtIndex:0];
}
UIImage * currentImage = pageView.canvas.image;
if (currentImage != nil) {
[oldImagesArrays addObject:currentImage];
undoRedoStackIndex = oldImagesArrays.count -1;
}
[...]
}
- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event {
UIImage * currentImage = [oldImagesArrays lastObject];
if (currentImage != pageView.canvas.image) {
[undoManager registerUndoWithTarget:self selector:@selector(resetImage)
object:currentImage];
}
}
- (void)undoDrawing
{
[undoManager undo];
[undoManager registerUndoWithTarget:self
selector:@selector(resetImage)
object:pageView.canvas.image];
undoRedoStackIndex--;
}
- (void)redoDrawing
{
[undoManager redo];
undoRedoStackIndex++;
}
- (void)resetImage
{
NSLog(@"Hello");
pageView.canvas.image = [oldImagesArrays objectAtIndex:undoRedoStackIndex];
}
, resetImage ( undoRedoStackIndex), , , .
&& || .