Question

I have few Core Data projects for MacOS started with Xcode 3 that behaves well in Xcode 4, especially for Undo/Redo management.
But now I started a new Core Data project from Xcode 4 and I'm facing problems with Undo: the Undo and Redo menu items are linked to the Undo:\Redo: methods of the First Responder, as happens for projects created by Xcode 3; but the menu was always disabled.
So I made a binding from the Enabled property of the menu items to managedObjectContext.undoManager.canUndo / canRedo: this enables the menu when the user makes a change to the objects, but anyway the undo action had no effect: the menu remains enabled but no undo was made.
The only way to obtain the undo was to break the connection of the menu items to the First Responder and instead make another one to new IBActions of the AppDelegate:

- (IBAction)myUndoAction:(id)sender  
{
    [[[self managedObjectContext] undoManager] undo];
}

- (IBAction)myRedoAction:(id)sender
{
    [[[self managedObjectContext] undoManager] undo];
}

In this way (and with the Enable properties bound to canUndo and canRedo) both menu items work as expected for Core Data objects; anyway I never had this problem with Xcode 3.

Does someone else face this problem with Xcode 4? I reinstalled Xcode 4.2.1 from scratch, to avoid old template problems, but I have the same problem again.
I don't like to install also Xcode 3, just to start with a functioning project, then migrate the project to Xcode 4!
I googled and looked in every forum I know, also Apple discussions, but I didn't find anything. I am on 10.7.2.


UPDATE
By linking Undo/Redo to the AppDelegate will break other operations, as the Undo/Redo will always call the NSUndoManager from the ManagedObjectContext, so the actions must remain linked to the First Responder.
I'll have to stick to Xcode 3.2 to generate a core data project from scratch, then open it from Xcode 4 (as far as I'll discover where the bug is, probably in the template...)

Was it helpful?

Solution

I was able to work around this for myself by making my window controller implement the NSWindowDelegate protocol and have it relay windowWillReturnUndoManager: calls to the application's delegate.

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