I’m just using a simple
self.customerTextField.text = @"text";
but I would like there to be undo support, where if they shake the phone they can undo the change.
I’m kinda at a loss as to how to accomplish this. All the exapmles I find are for UITextView.
======================== Current iteration of code ======================
-(void)getUniqueItemID {
[self.inventoryLibrarian generateUniqueItemIDwithCompletionBlock:^(NSString *string) {
[self undoTextFieldEdit:string];
//self.customTextField.text = string;
}];
}
- (void)undoTextFieldEdit: (NSString*)string
{
[self.undoManager registerUndoWithTarget:self
selector:@selector(undoTextFieldEdit:)
object:self.customTextField.text];
self.customTextField.text = string;
}
-(BOOL)canBecomeFirstResponder {
return YES;
}
Shake-to-undo
Put this line in your appDelegate’s
application:didFinishLaunchingWithOptions:method:and in the relevant viewController.m
the rest of this code goes in viewController.m
Property
Put this in the class extension…
link it up to your text field in Interface Builder.
Undo method
Adds an invocation of itself to the redo stack
(we do not need to create an NSUndoManager instance, we inherit one from the UIResponder superclass)
Undo actions
Not required for shake-to-undo, but could be useful…
Invocations of the undo method
Here are two different examples of changing the textField’s contents…
Example 1
Set the textField’s contents from a button action
Risking loss of clarity, we can shorten this to:
as the undoManager invocation is the same in both methods
Example 2
Direct keyboard input editing