如何合并您自己的撤消操作? 这是最简单的方法。
注意:这不会响应模拟器中的摇晃手势。
UndoTestViewController.h:
@interface UndoTestViewController : UIViewController <UITextViewDelegate, UIAccelerometerDelegate>{
IBOutlet UITextView *textview;
NSString *previousText;
BOOL showingAlertView;
}
@property (nonatomic,retain) NSString *previousText;
@end
UndoTestViewController.m:
@implementation UndoTestViewController
@synthesize previousText;
- (void)viewDidLoad {
[super viewDidLoad];
//disable built-in undo
[UIApplication sharedApplication].applicationSupportsShakeToEdit = NO;
showingAlertView = NO;
[UIAccelerometer sharedAccelerometer].delegate = self;
[UIAccelerometer sharedAccelerometer].updateInterval = kUpdateInterval;
//set initial undo text
self.previousText = textview.text;
}
#pragma mark UITextViewDelegate
- (BOOL)textView:(UITextView *)textView shouldChangeTextInRange:(NSRange)range replacementText:(NSString *)text {
//save text before making change
self.previousText = textView.text;
//changing text in some way...
textView.text = [NSString stringWithFormat:@"prepending text %@",textView.text];
[textView resignFirstResponder];
return YES;
}
#pragma mark -
#pragma mark UIAccelerometerDelegate
- (void)accelerometer:(UIAccelerometer *)accelerometer didAccelerate:(UIAcceleration *)acceleration {
if( showingAlertView ) return;
if ( acceleration.x > kAccelerationThreshold ||
acceleration.y > kAccelerationThreshold ||
acceleration.z > kAccelerationThreshold ) {
showingAlertView = YES;
NSLog(@"x: %f y:%f z: %f", acceleration.x, acceleration.y, acceleration.z);
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"" message:@"" delegate:self cancelButtonTitle:@"Cancel" otherButtonTitles:@"Undo Typing", nil];
alert.delegate = self;
[alert show];
[alert release];
}
}
#pragma mark -
#pragma mark UIAlertViewDelegate
- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex {
if( buttonIndex == 1 ) {
textview.text = self.previousText;
}
showingAlertView = NO;
}
#pragma mark -