core data support undo manager

- (NSManagedObjectContext *)managedObjectContext {
if (managedObjectContext_ != nil) {
return managedObjectContext_;
}
NSPersistentStoreCoordinator *coordinator = [self persistentStoreCoordinator];
if (coordinator != nil) {
managedObjectContext_ = [[NSManagedObjectContext alloc] init];
[managedObjectContext_ setPersistentStoreCoordinator:coordinator];

NSUndoManager *undoManager = [[NSUndoManager alloc] init];

[undoManager setLevelsOfUndo:10];

[managedObjectContext_ setUndoManager:undoManager];
[undoManager release];

}
return managedObjectContext_;

}


#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.

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值