Shake-to-undo working on CoreData app, but no undo/redo prompts shown
-
26-09-2019 - |
Question
I have a CoreData based application which supports shake-to-undo. This is currently working fine, however when I shake the device, the undo happens immediately, without first showing the usual undo prompt which NSUndoManager normally provides for free.
I've read "Using Undo on iPhone" from the "Undo Architecture" document and am:
- implementing
canBecomeFirstResponder
, returningYES
- making the view controller
becomeFirstResponder
inviewDidAppear
- making the view controller
resignFirstResponder
inviewDidDisappear
- redeclaring the
self.undoManager
in the view controller as read/write - creating an
NSUndoManager
and settingself.undoManager
in theUIViewController
init
method (which I've verified is getting called). - setting
self.managedObjectModel.managedObjectContext.undoManager
toself.undoManager
in the setter formanagedObjectModel
. Again, I've verified this is getting called. If I don't do this, undo doesn't work at all.
Troubleshooting I've already tried:
- checking undo does in fact work (I see changes to my managedObjectModel reverted)
- verifying
canBecomeFirstResponder
is being called - checking if accessor
self.undoManager
is being called during an undo operation (which it is not).
So my questions are:
- under what circumstances does undo work but not show the undo prompts?
- What would you try next to troubleshoot this?
Solution
I eventually tracked this down to some legacy code which was manually catching the shaking motion and calling undo on the undoManager; an implementation of motionEnded: in the view controller. Once this was removed, I started getting undo prompts again.
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow