无论如何,我找到了解决方案。
默认的撤消管理器实际上仍然在那里秘密地保存对 UITextView 所做的所有操作。
由于UIWindow
接管了运动处理,首先我尝试通过覆盖motionEnded:withEvent:
包含我的UITextView
.
其次,获取 undoManager ,[myTextView undoManager]
然后您可以向它发送undo
或redo
消息。
现在模仿默认的撤消管理器警报视图,使用redoMenuItemTitle
andundoMenuItemTitle
获取按钮标题,然后使用canUndo
andcanRedo
决定是否显示按钮。
编辑:这是我的应用程序中的代码:
- (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];
}
}