Anyway I found a solution.
The default undo manager is actually still there saving all actions done to a UITextView secretly.
Since UIWindow
takes over the motion handling, first I try to take it back by overriding motionEnded:withEvent:
in the viewController that contains my UITextView
.
Second, get the undoManager by [myTextView undoManager]
then you can send undo
or redo
messages to it.
Now to mimic the default undo manager alertview, use redoMenuItemTitle
and undoMenuItemTitle
to get button title, then use canUndo
and canRedo
to decide whether to show button or not.
Edit: Here's the code from my application:
- (void)motionEnded:(UIEventSubtype)motion withEvent:(UIEvent *)event {
if (event.type == UIEventTypeMotion && event.subtype == UIEventSubtypeMotionShake) {
//textUndoManager is an ivar but it was just a reference to the undoManager
//textUndoManager = [myTextView undoManager]; <--- in viewDidLoad:
NSString *undoButtonTitle = nil;
NSString *redodoButtonTitle = nil;
NSString *alertViewTitle = nil;
if ([textUndoManager canUndo])
undoButtonTitle = [NSString stringWithString:[textUndoManager undoMenuItemTitle]];
if ([textUndoManager canRedo])
redodoButtonTitle = [NSString stringWithString:[textUndoManager redoMenuItemTitle]];
if (!undoButtonTitle && !redodoButtonTitle)
alertViewTitle = @"Nothing to Undo";
UIAlertView *alertView;
if (undoButtonTitle == nil) {
alertView = [[UIAlertView alloc] initWithTitle:alertViewTitle message:nil delegate:self cancelButtonTitle:@"Cancel" otherButtonTitles:redodoButtonTitle, nil];
} else {
alertView = [[UIAlertView alloc] initWithTitle:alertViewTitle message:nil delegate:self cancelButtonTitle:@"Cancel" otherButtonTitles:undoButtonTitle, redodoButtonTitle, nil];
}
[alertView show];
}
}
//UIAlertViewDelegate
- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex {
if ([[alertView buttonTitleAtIndex:buttonIndex] isEqualToString:[textUndoManager undoMenuItemTitle]]) {
[textUndoManager undo];
}
if ([[alertView buttonTitleAtIndex:buttonIndex] isEqualToString:[textUndoManager redoMenuItemTitle]]) {
[textUndoManager redo];
}
}