Note that there are some explanatory texts on larger screens.

plurals
  1. PONSUndoManager calls my "restore" method repeatedly
    primarykey
    data
    text
    <p>I have a <code>UITextView</code> with a formatting toolbar. After I press the "bold" button, it changes the text/selection and registers the operation with the text view's <code>NSUndoManager</code>.</p> <p>When you try to undo, <code>restoreText:</code> is called over and over until you kill the app</p> <p>Some sample code:</p> <pre><code>- (void)bold { NSRange range = self.textView.selectedRange; NSRange oldRange = range; NSString *oldText = self.textView.text; NSString *selection = [self.textView.text substringWithRange:range]; self.textView.scrollEnabled = NO; self.textView.text = [self.textView.text stringByReplacingCharactersInRange:range withString:[NSString stringWithFormat:@"&lt;b&gt;%@&lt;/b&gt;", selection]]; self.textView.scrollEnabled = YES; if (range.length == 0) { // If nothing was selected range.location += 3; // Place selection between tags } else { range.location += range.length + 7; // Place selection after tag range.length = 0; } self.textView.selectedRange = range; [[self.textView.undoManager prepareWithInvocationTarget:self] restoreText:oldText withRange:oldRange]; [self.textView.undoManager setActionName:@"bold"]; } - (void)restoreText:(NSString *)text withRange:(NSRange)range { NSLog(@"restoreText:%@ %@",text, [NSNumber numberWithBool:[self.textView.undoManager isUndoing]]); NSString *oldText = self.textView.text; NSRange oldRange = self.textView.selectedRange; self.textView.scrollEnabled = NO; self.textView.text = text; self.textView.scrollEnabled = YES; self.textView.selectedRange = range; [[self.textView.undoManager prepareWithInvocationTarget:self] restoreText:oldText withRange:oldRange]; } </code></pre> <p>And the console:</p> <pre><code>2012-10-04 18:10:14.207 undotest[8861:c07] restoreText: 1 2012-10-04 18:10:14.633 undotest[8861:c07] restoreText:&lt;b&gt;&lt;/b&gt; 0 2012-10-04 18:10:15.117 undotest[8861:c07] restoreText: 0 2012-10-04 18:10:15.589 undotest[8861:c07] restoreText:&lt;b&gt;&lt;/b&gt; 0 2012-10-04 18:10:16.017 undotest[8861:c07] restoreText: 0 2012-10-04 18:10:16.557 undotest[8861:c07] restoreText:&lt;b&gt;&lt;/b&gt; 0 </code></pre> <p>I've tried checking for <code>[self.textView.undoManager isUndoing]</code> before registering the invocation on <code>restoreText:withRange:</code>, but that crashes:</p> <pre><code>2012-10-04 18:10:49.297 undotest[8904:c07] restoreText: 1 2012-10-04 18:10:53.412 undotest[8904:c07] *** Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: '_endUndoGroupRemovingIfEmpty:: WebThreadSafeUndoManager 0x76551e0 is in invalid state, endUndoGrouping called with no matching begin ' </code></pre> <p>This code was working on iOS5 but stopped working on iOS6. I've filed a bug with Apple, but I'm not sure if I'm doing something wrong</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload