Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p><strong>Shake-to-undo</strong><br> Put this line in your appDelegate's <code>application:didFinishLaunchingWithOptions:</code> method:</p> <pre><code> application.applicationSupportsShakeToEdit = YES; </code></pre> <p>and in the relevant viewController.m</p> <pre><code> -(BOOL)canBecomeFirstResponder { return YES; } </code></pre> <p>the rest of this code goes in viewController.m</p> <p><strong>Property</strong><br> Put this in the class extension...</p> <pre><code> @interface myViewController() @property (weak, nonatomic) IBOutlet UITextField *inputTextField; @end </code></pre> <p>link it up to your text field in Interface Builder.</p> <p><strong>Undo method</strong><br> Adds an invocation of itself to the redo stack</p> <pre><code> - (void)undoTextFieldEdit: (NSString*)string { [self.undoManager registerUndoWithTarget:self selector:@selector(undoTextFieldEdit:) object:self.inputTextField.text]; self.inputTextField.text = string; } </code></pre> <p>(we do not need to create an NSUndoManager instance, we inherit one from the UIResponder superclass)</p> <p><strong>Undo actions</strong><br> Not required for shake-to-undo, but could be useful...</p> <pre><code> - (IBAction)undo:(id)sender { [self.undoManager undo]; } - (IBAction)redo:(id)sender { [self.undoManager redo]; } </code></pre> <p><strong>Invocations of the undo method</strong><br> Here are two different examples of changing the textField's contents…</p> <p><em>Example 1</em><br> Set the textField's contents from a button action</p> <pre><code> - (IBAction)addLabelText:(UIButton*)sender { [self.undoManager registerUndoWithTarget:self selector:@selector(undoTextFieldEdit:) object:self.inputTextField.text]; self.inputTextField.text = @"text"; } </code></pre> <p>Risking loss of clarity, we can shorten this to:</p> <pre><code> - (IBAction)addLabelText:(UIButton*)sender { [self undoTextFieldEdit: @"text"]; } </code></pre> <p>as the undoManager invocation is the same in both methods</p> <p><em>Example 2</em><br> Direct keyboard input editing</p> <pre><code> #pragma mark - textField delegate methods - (BOOL)textFieldShouldBeginEditing:(UITextField *)textField { [self.undoManager registerUndoWithTarget:self selector:@selector(undoTextFieldEdit:) object:textField.text]; return YES; } - (BOOL)textFieldShouldReturn:(UITextField *)textField { //to dismiss the keyboard [textField resignFirstResponder]; return YES; } </code></pre>
    singulars
    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. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      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