//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):
myUndoManager
undoManager
method returning myManagedObjectContext.undoManager;
viewDidAppear
: myManagedObjectContext.undoManager = myUndoManager;
//create first if nilviewWillDisappear
: myManagedObjectContext.undoManager = nil;
[self.undoManager removeAllActions ];
self.myUndoManager = nil;
[self.undoManager setActionName:NSLocalizedString(@“XXX”,@“”)];
In addition, I have to remain firstResponder:
viewDidAppear
: `[self becomeFirstResponder]'canBecomeFirstResponder
method returning YESviewWillDisappear
: [self resignFirstResponder];So far, that seems like it works, even across load/unload cycles, and is nicely self-contained, but I have several questions:
I would work very hard to have only one undo manager.
consider the scenario:
model: (chicken)
properties: eggs, color, size;
model: (egg)
properties: chicken, color;
chicken.eggs = one to many relationship to egg, inverse is true of egg.chicken.
you create 2 undo managers one for chickenViewController, and one for eggViewController.
you create chicken0 and its eggs: egg0, egg1.
you create chicken1 and its eggs: egg2, egg3.
you delete egg2.
now you delete chicken1 cascade deleting the eggs.
now you go back to eggViewController and undo... what do you want to happen (exception would happen).
now you undo chickenViewController and chicken1 and it's eggs come back, but are there 2 egg2's?
Edit I am softening my tone on this a little, assuming that you are using a hierarchical view structure, like UINavigationController, and you make a new Undo controller each time that you go to a child view, I don't thing you should have a problem.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With