Cancel input in UITextView

I am using iPhone SDK 3.0.

Say I turned on 'shake to undo' by setting application.applicationSupportsShakeToEdit to YES in my application delta. I created a UITextView in the RootViewController and became the first responder when I started the application.

I use - (BOOL)textView:(UITextView *)aTextView shouldChangeTextInRange:(NSRange)range replacementText:(NSString *)text to make changes to the text representation according to what the user enters. The problem is that he confused the default undo action. Shaking the device no longer displays "Cancel Entry."

I tried to create a cancellation manager, but have not had time yet. Nothing appears when I shake my device, although I had a cancel manager that logged some prepareWithInvocationTarget action.

Does anyone have an idea?

EDIT . I checked WriteRoom for iPhone behavior, as well as the sample code for the TextExpander Touch SDK sample code, both of which display the Undo option when shaking until they complete automatically. That is, when a change is carried forward to textView via setText :, I think. Thus, changing the contents of a textView does affect the undo mechanism.

EDIT 2 : The question is: how could I enable the undo function in this case?

+4
source share
5 answers

I figured out a solution to my problems - he outlined here in the iPhoneDevDude answer .

0
source

How to enable your own shutdown? . This is the easiest way.

NOTE: this does not respond to the shake gesture in the simulator.

UndoTestViewController.h:

 @interface UndoTestViewController : UIViewController <UITextViewDelegate, UIAccelerometerDelegate>{ IBOutlet UITextView *textview; NSString *previousText; BOOL showingAlertView; } @property (nonatomic,retain) NSString *previousText; @end 

UndoTestViewController.m:

 @implementation UndoTestViewController @synthesize previousText; - (void)viewDidLoad { [super viewDidLoad]; //disable built-in undo [UIApplication sharedApplication].applicationSupportsShakeToEdit = NO; showingAlertView = NO; [UIAccelerometer sharedAccelerometer].delegate = self; [UIAccelerometer sharedAccelerometer].updateInterval = kUpdateInterval; //set initial undo text self.previousText = textview.text; } #pragma mark UITextViewDelegate - (BOOL)textView:(UITextView *)textView shouldChangeTextInRange:(NSRange)range replacementText:(NSString *)text { //save text before making change self.previousText = textView.text; //changing text in some way... textView.text = [NSString stringWithFormat:@"prepending text %@",textView.text]; [textView resignFirstResponder]; return YES; } #pragma mark - #pragma mark UIAccelerometerDelegate - (void)accelerometer:(UIAccelerometer *)accelerometer didAccelerate:(UIAcceleration *)acceleration { if( showingAlertView ) return; if ( acceleration.x > kAccelerationThreshold || acceleration.y > kAccelerationThreshold || acceleration.z > kAccelerationThreshold ) { showingAlertView = YES; NSLog(@"x: %fy:%fz: %f", acceleration.x, acceleration.y, acceleration.z); UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"" message:@"" delegate:self cancelButtonTitle:@"Cancel" otherButtonTitles:@"Undo Typing", nil]; alert.delegate = self; [alert show]; [alert release]; } } #pragma mark - #pragma mark UIAlertViewDelegate - (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex { if( buttonIndex == 1 ) { textview.text = self.previousText; } showingAlertView = NO; } #pragma mark - 
+4
source

The default value for applicationSupportsShakeToEdit is YES , so there really should not be a need to enable this.

I don’t know for sure that your question / problem is without code, but it is possible by returning NO to a UITextViewDelegate textView: shouldChangeTextInRange you can short-circuit the magic of UndoManager. You may need to look at this logic inside this delegate method to make sure this does not happen, or maybe use another delegate message, for example textViewDidChange: .

+1
source

I found that the implementation, at least in version 3.1, was a mistake and decided not to use undo at all in my application. The error included a cancellation mechanism, adding additional cancellations in certain circumstances, so the message displayed to the user does not make any sense.

0
source

You looked at the source code here: http://github.com/dcgrigsby/TallyTrucks/tree/master It has a basic (working) implementation of NSUndoManager. Perhaps this may help.

0
source

All Articles