//Edit: Really, nobody has any suggestions or thoughts on this? Have I asked the question wrongly somehow?//
My iPhone app has a single managedObjectContext with a moderately complicated data model. I'm now adding undo functionality, and am not clear on how best to handle nested viewControllers (as each layer might modify the data model).
Apple's docs point out: "Consider an application that displays a list of books, and allows you to navigate to a detail view that in turn allows you to edit individual properties of the book (such as its title, author, and copyright date). You might create a new book from the list screen, navigate between two other screens to edit its properties, then navigate back to the original list. It might seem peculiar if an undo operation in the list view undid a change to the author’s name that was made two screens away rather than deleting the entire book."
So what's the best way to implement this? Currently, I'm thinking to have each viewController keep its own undoManager, which would be active whenever it's on the screen. So my understanding is that this would require the following steps (for each VC):
- Add a property:
myUndoManager
- Add an
undoManager
method returningmyManagedObjectContext.undoManager;
- In
viewDidAppear
:myManagedObjectContext.undoManager = myUndoManager;
//create first if nil - In
viewWillDisappear
:myManagedObjectContext.undoManager = nil;
- On memory warning:
[self.undoManager removeAllActions ];
- On dealloc:
self.myUndoManager = nil;
- For each model change:
[self.undoManager setActionName:NSLocalizedString(@“XXX”,@“”)];
- CoreData will handle the actual undo/redo postings
In addition, I have to remain firstResponder:
- In
viewDidAppear
: `[self becomeFirstResponder]' - Add
canBecomeFirstResponder
method returning YES - In
viewWillDisappear
: [self resignFirstResponder]; - Re-enable firstResponder upon subViews resign (e.g. textFields)
So far, that seems like it works, even across load/unload cycles, and is nicely self-contained, but I have several questions:
- First, is this the best practice for implementing undo across multiple VCs?
- Will I get in trouble with my child VCs not doing their undos prior to my doing my earlier ones?
- If so, does that list capture everything I need to do?
- Will ManagedObjectContext get confused with multiple UndoManagers being active?
- Do I need to call ProcessPendingActions before swapping undoManagers?