NSUndoManager *undoManager = [[NSUndoManager alloc] init];
[undoManager setLevelsOfUndo:10];
}
#import <UIKit/UIKit.h>
#import <CoreData/CoreData.h>
#import "BasicCanvasUIView.h"
@interface ShapesViewController : UIViewController {
NSManagedObjectContext *managedObjectContext;
IBOutlet BasicCanvasUIView *topView;
IBOutlet BasicCanvasUIView *bottomView;
IBOutlet UIButton *undoButton;
IBOutlet UIButton *redoButton;
}
@property (nonatomic, retain) NSManagedObjectContext *managedObjectContext;
@property (nonatomic, retain) BasicCanvasUIView *topView;
@property (nonatomic, retain) BasicCanvasUIView *bottomView;
@property (nonatomic, retain) UIButton *undoButton;
@property (nonatomic, retain) UIButton *redoButton;
- (void)deleteAllShapes;
- (void)updateAllShapes;
- (IBAction)undo:(id)sender;
- (IBAction)redo:(id)sender;
- (void)updateUndoAndRedoButtons:(NSNotification *)notification;
@end
Add @synthesize directives to ShapesViewController.m for undoButton and redoButton.
Open ShapesViewController.xib, and drag two Round Rect Button instances to the top
corners of the screen----one on the left and one on the right. Change the label on the one
on the left to Undo and the one on the right to Redo. Set the Autosizing appropriately for
each button. The Undo button should have the bands on the top and left only, and the
Redo button should have the bands on the top and right only. Ctrl+drag from the File’s
Owner icon to the Undo button, and wire it to undoButton; repeat the process to wire the
Redo button to redoButton. Wire the Touch Up Inside events for each button to the
methods you created----the Undo button to the undo: method and the Redo button to
the redo: method.
The undo method should get the undo manager from the managed object context and
call the undo: method and then tell the views to repaint themselves, like this:
- (IBAction)undo:(id)sender {
[[self.managedObjectContext undoManager] undo];
[topView setNeedsDisplay];
[bottomView setNeedsDisplay];
}
CHAPTER 5: Working with Data Objects
179
179
The redo: method should do the same thing but call the undo manager’s redo: method
instead of its undo: method:
- (IBAction)redo:(id)sender {
[[self.managedObjectContext undoManager] redo];
[topView setNeedsDisplay];
[bottomView setNeedsDisplay];
}
To show the Undo button only when the user can undo a change and the Redo button
only when the user can redo a change, you could insert code everywhere you make data
changes to update the buttons. Take advantage of Cocoa Touch’s notification
mechanism, however, and have the managed object context notify you whenever its
managed data changes. In the viewDidLoad: method, add code to call your
updateUndoAndRedoButtons: method any time the data changes. You also want to call
the updateUndoAndRedoButtons: method when the view first loads, so tack on a call to
that method. This is the code you should add to viewDidLoad:
// Register for changes to the managed object context
NSNotificationCenter *notificationCenter = [NSNotificationCenter defaultCenter];
[notificationCenter addObserver:self selector:@selector(updateUndoAndRedoButtons:)
name:NSManagedObjectContextObjectsDidChangeNotification object:nil];
[self updateUndoAndRedoButtons:nil];
In the viewDidUnload: method, undo the notifications:
- (void)viewDidUnload {
[[NSNotificationCenter defaultCenter] removeObserver:self];
}
The updateUndoAndRedoButtons: method hides the buttons appropriately when the
application has nothing to undo or redo. It looks like this:
- (void)updateUndoAndRedoButtons:(NSNotification *)notification {
NSUndoManager *undoManager = [self.managedObjectContext undoManager];
undoButton.hidden = ![undoManager canUndo];
redoButton.hidden = ![undoManager canRedo];
}
After making these changes, build and run the Shapes application. As you add shapes
and undo changes, you should see the Undo and Redo buttons as in Figure 5-12. Add
shapes, change the colors of shapes by rotating the device, and delete the shapes.
Click Undo and Redo and see the data changes undo and redo themselves.