I created a managed object with several members of 开发者_如何转开发NSNumber in my iPhone app. I initialize these values. I also create a sub-object member. Then, I create an undo manager so that I can perform undo operations on the sub-object, without undo affecting the initialized paarameters. However, when I start my app and call...
[undoManager canUndo];
... it is "YES". How can this be? The top-level data members are reset to zero. Not only that, but the managedObjectContext of sub-objet is nil. How can this be when the sub-object is created PRIOR TO creation of the undo manager. Is the undo manager retro-active? It appears to allow undo of the entire managed object even though these data members are set BEFORE I create the undo manager.
Any insight is appreciated.
It appears that if a managed object is not saved then the creation of an NSUndoManager applies to the object as it exists, and will allow undo of any changes to the object. I found this by simply saving the object prior to the view appearing, as the object is created from options on a variety of screens. If I try to undo after saving the object, canUndo returns NO, as expected.
Another option is to somehow call removeAllActions on the undo manager after the view appears but this would be problematic because there are too many entry points. Now that the object is saved, if the user cancels I will delete it.
I hope this helps someone in the future.
The registration of the changes at the undo manager is deferred. You must ensure that changes are processed until you set the undo manager. You can use the processPendingChanges method. Here little example:
// create initial objects
[managedObjectContext processPendingChanges];
// enable undo
managedObjectContext.undoManager = undoManager;
精彩评论