Undo/redo with a UITextView (iOS/iPHone)

后端 未结 3 1798
逝去的感伤
逝去的感伤 2020-12-15 01:49

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 for when I programmatically set the text

3条回答
  •  时光取名叫无心
    2020-12-15 02:18

    OK, figured it out:

    The issue with #1 is as outlined in the update to my original post.

    Issues #2 and #3 were just me using the NSUndoManager incorrectly. My final unClear method (which gets called on undos is as follows:

    -(void)undoClear:(NSString *)textToRestore
    {   
        NSString *textBackup = [self.myTextView.text copy];
    
        self.myTextView.text = textToRestore;
    
        if ([self.myTextView.undoManager isUndoing])
        {
            // Prepare the redo.
            [[self.myTextView.undoManager prepareWithInvocationTarget:self] undoClear:@""];     
        }
        else if ([self.myTextView.undoManager isRedoing])
        {
            [[self.myTextView.undoManager prepareWithInvocationTarget:self] undoClear:textBackup];
        }
    
        [textBackup release];
    }

    It's working as it should now.

提交回复
热议问题