I'm going to describe what I'm trying to do generally (in case there's a better way) and then the stumbling block I've run into (in case my way is the best way).
What I want to do: I want to add an invocation to my undo manager that has a time limit. If the undo is not triggered within the time limit, it won't be available when the device is shaken and thus nothing will happen.
What I'm doing: My approach was to use an NSUndoManager
with an NSTimer
. When I add the invocation to the undo manager, I kick off a 5-second timer as well. When the timer fires it checks !self.undoManager.isUndoing
and if it's true than it goes ahead and removes all actions from the undo manager. Testing it in the simulator works: a shake gesture kicks off the undo before 5 seconds, but not after.
The problem is that if I get a shake gesture under the 5 second limit, the undo manager shows the alert, but if the user waits until after the 5s limit to actually tap the undo button, nothing happens: the timer happily cleared the stack away, even though the alert view was visible.
Is there a way to check and see if the alert view is visible? Best would be if I could figure out if the user hit undo or cancel as well, and clear the undo manager's action stack if the cancel button was pressed.
Or is there a better way besides using a timer in this manner?
Thanks!
Edited to add: My other thought was to capture the shake event myself (via the motionEnded:withEvent:
call) and manually manage the alert and undo stack. Thoughts on this compared to the above are also welcome.