Question

I have a complex situation where I have a view controller and several sub view controllers.

Situation A: When I change the sub view controller, I record the change in NSUndoManager, and the undo calls the undo method in the sub view controller and works fine.

-(void)setElementTransform:(NSString *)transform
{
    NSUndoManager *undoManager = [Global sharedGlobalInstance].undoMgr;
    [undoManager registerUndoWithTarget:self selector:@selector(setElementTransform:) object:self.element.transform];
    self.element.transform = transform;
    self.view.transform = CGAffineTransformFromString(transform);
}

Situation B: This is a bit more complex - when I remove the sub view controller, I record the undo method (in the main view controller) in NSUndoManager. When I undo that remove, the NSUndoManager calls the undo method in the main view controller, which recreates the sub view controller. That works fine too.

The problem is that if I do Situation A followed by Situation B, I have two in the undo stack. I can undo Situation B, but Situation A has reference to an undo method in a sub view controller that no longer exists (it was removed and recreated by deleting and undoing in Situation B).

I don't think there is any way of updating NSUndoManager so that I can change the reference to the old sub view controller?

How else could I cope with this?

Was it helpful?

Solution

You could register all undo actions with an object that will not be discarded. For example on the top level view controller, or even the app delegate, or a separate singleton. Then you would need to include in the object argument of registerUndoWithTarget:selector:object: enough information to apply the undo action on the appropriate sub view controller or its replacement.

If you just need to get actions targeted at the dead sub view controller out of the undo stack, call removeAllActionsWithTarget:self in the dealloc (or viewWillDisappear, or viewDidUnload, as appropriate) method of the sub view controller.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top