gpt4 book ai didi

iphone - 撤消 UITextView 中的输入

转载 作者:行者123 更新时间:2023-12-03 19:13:01 25 4
gpt4 key购买 nike

我正在使用 iPhone SDK 3.0。

比如说,我通过设置 application.applicationSupportsShakeToEdit 启用了“摇动撤消”功能至YES在我的应用程序委托(delegate)中。我在 RootViewController 中创建了一个 UITextView 并让它成为应用程序启动时的第一响应者。

我使用- (BOOL)textView:(UITextView *)aTextView shouldChangeTextInRange:(NSRange)range replacementText:(NSString *)text根据用户键入的内容更改 TextView 。问题是它搞乱了默认的撤消操作。摇动设备不再显示“撤消键入”。

我尝试自己创建一个撤消管理器,但尚未成功。即使我让撤消管理器注册了一些操作,当我摇动设备时,也没有显示任何内容 prepareWithInvocationTarget .

有人有想法吗?

编辑:我已经检查了 WriteRoom for iPhone 的行为以及 TextExpander Touch SDK 示例代码,两者都在摇动时显示“撤消”选项,直到自动完成为止。也就是说,当通过 setText: 将更改提交给 textView 时,我猜。所以,改变textView的内容确实会对undo机制产生一些影响。

编辑2:问题是:在这种情况下我如何合并撤消功能?

最佳答案

如何合并您自己的撤消?这是最简单的方法。

注意:这不会响应模拟器中的摇动手势。

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: %f y:%f z: %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 -

关于iphone - 撤消 UITextView 中的输入,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1991897/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com