undo / redo如何在iPhone OS上运行? [英] How does undo/redo basically work on iPhone OS?

查看:120
本文介绍了undo / redo如何在iPhone OS上运行?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的应用尚未使用Core Data。我必须使用Core Data进行撤消/重做吗?

My app doesn't use Core Data yet. Is it true that I must use Core Data for undo/redo?

并且:用户如何进行撤消/重做?我从来没有见过它,从来没有使用它。如果我愿意,不知道我应该怎么做。任何地方都没有撤消/重做按钮。然而他们说它有撤消/重做。那么用户如何触发这个呢?

And: How does the user do the undo/redo? I've never seen it in action, and never ever used it. Don't know how I should do it if I wanted to. There's no undo/redo button anywhere. Yet they say it has undo/redo. So how does the user trigger this?

推荐答案

iPhone OS 3.0从Mac带来了NSUndoManager的概念,这是什么启用在iPhone上撤消。 NSUndoManager维护一组NSInvocations,这些操作与您所做的任何编辑或其他更改相反。例如,

iPhone OS 3.0 brought over the concept of NSUndoManager from the Mac, which is what enables undo on the iPhone. NSUndoManager maintains a stack of NSInvocations which are the opposite actions to any edits or other changes you make. For example,

- (void)observeValueForKeyPath:(NSString*)keyPath
                      ofObject:(id)object
                        change:(NSDictionary*)change
                       context:(void*)context
{
    NSUndoManager *undo = [self undoManager];
    // Grab the old value of the key
    id oldValue = [change objectForKey:NSKeyValueChangeOldKey];
    // Add edit item to the undo stack
    [[undo prepareWithInvocationTarget:self] changeKeyPath:keyPath 
                                                  ofObject:object 
                                                   toValue:oldValue];
    // Set the undo action name in the menu
    [undo setActionName:@"Edit"];
}

可用于观察属性的变化,创建撤消编辑的逆NSInvocations这些属性。

can be used to observe changes in properties, creating inverse NSInvocations that will undo edits to those properties.

撤消时不需要核心数据,但它更容易实现。每次编辑数据模型时,它都会为您处理这些撤消操作的创建,包括复杂的操作,如级联删除托管对象的层次结构。

Core Data is not needed for undo, but it makes it much, much easier. It handles the creation of these undo actions for you every time you edit your data model, including complex actions like a cascading delete down a hierarchy of managed objects.

在iPhone上,要启用撤消/重做,您需要设置一些东西。首先,iPhone上的NSManagedObjectContexts默认没有撤消管理器,因此您需要创建一个:

On the iPhone, to enable undo / redo, you need to set up a few things. First, NSManagedObjectContexts on the iPhone don't have an undo manager by default, so you need to create one:

NSUndoManager *contextUndoManager = [[NSUndoManager alloc] init];
[contextUndoManager setLevelsOfUndo:10];
[managedObjectContext setUndoManager:contextUndoManager];
[contextUndoManager release];       

此代码通常会在您创建NSManagedObjectContext之后立即执行。

This code would typically go right after where you would have created your NSManagedObjectContext.

为您的上下文提供撤消管理器后,您需要在iPhone上启用撤消的默认手势,即摇动设备。要让您的应用程序自动处理此手势,请将以下代码放在应用程序委托中的 -applicationDidFinishLaunching:方法中:

Once an undo manager is provided for your context, you need to enable the default gesture for undo on the iPhone, a shake of the device. To let your application handle this gesture automatically, place the following code within the -applicationDidFinishLaunching: method in your application delegate:

application.applicationSupportsShakeToEdit = YES;

最后,您需要设置每个能够处理摇动手势的视图控制器撤消。这些视图控制器需要通过覆盖 -undoManager 方法来报告用于该控制器的撤消管理器:

Finally, you will need to set up each view controller that will be capable of handling the shake gesture for undo. These view controllers will need to report back the undo manager to use for that controller by overriding the -undoManager method:

- (NSUndoManager *)undoManager;
{
    return [[[MyDatabaseController sharedDatabaseController] scratchpadContext] undoManager];
}

视图控制器还需要能够成为第一个处理的响应者手势,所以需要以下方法:

The view controllers will also need to be able to become the first responder to handle gestures, so the following method is needed:

- (BOOL)canBecomeFirstResponder 
{
    return YES;
}

视图控制器在屏幕上显示时需要成为第一个响应者。这可以通过在 -loadView -viewDidLoad中调用 [self becomeFirstResponder] 来完成。 code>,但我发现启动后立即出现在屏幕上的视图控制器需要延迟此消息才能使其正常工作:

The view controller will need to become the first responder when it appears onscreen. This can be done by calling [self becomeFirstResponder] in -loadView or -viewDidLoad, but I have found that view controllers which appear onscreen immediately after launch need to have this message delayed a bit in order for it to work:

[self performSelector:@selector(becomeFirstResponder) withObject:nil afterDelay:0.3];

完成所有这些后,您应该获得Core Data的自动撤消和重做支持,漂亮的动画菜单。

With all this in place, you should get automatic undo and redo support courtesy of Core Data, with a nice animated menu.

这篇关于undo / redo如何在iPhone OS上运行?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆