I have a view where a UITextView always has focus. What I want to do is extend the built-in undo / redo behavior to support undo / redo when I programmatically set the text (for example, when I clear it by setting it to @ "").
Since only firstResponder receives cancel / redo events, I thought I was just using the UITextView undoManager property to create my calls. eg,
// Before clearing the text ... [[self.myTextView.undoManager prepareWithInvocationTarget: self] undoClear: self.myTextView.text]; [self.myTextView.undoManager setActionName: @ "Clear"]; // ... - (void) undoClear: (NSString *) textToRestore {self.myTextView.text = textToRestore; if ([self.myTextView.undoManager isUndoing]) {// Prepare the redo. [[self.myTextView.undoManager prepareWithInvocationTarget: self] undoClear: @ ""]; }} Unfortunately this does not work. It:
- Inserts an empty item into the cancel stack ("Undo")
- After this item, “Undo Clear” is added (if I click Undo, I see “Undo Clear”)
- Cancel Clear and Repeat Clear work, however, I see “Cancel Clear” again, and it does not work from there.
Any ideas? Am I approaching this wrong?
Update: Looks like I figured out the issue with the empty release: this happens when I set the text to a UITextView after I call prepareWithInvocationTarget. If I call earlier, this will not happen. Funny, an empty element is not pushed onto the cancel stack unless I call prepareWithInvocationTarget (i.e., usually when I set the text to a UITextView).
source share