我在UITextField中添加了UILongPressGestureRecognizer。当我按下UITextField时,它会显示警报,但这是三个警报显示我。 那是我的代码:
- (void)viewDidLoad
{
[super viewDidLoad];
UILongPressGestureRecognizer *gs = [[UILongPressGestureRecognizer alloc]initWithTarget:self action:@selector(AlertServer:)];
gs.delegate = self;
[_companyidTxt addGestureRecognizer:gs];
[gs release];
}
-(void)AlertServer:(UILongPressGestureRecognizer *)gs
{
alertView = [[UIAlertView alloc] initWithTitle:@"Server" message:@"" delegate:self cancelButtonTitle:@"Cancel" otherButtonTitles:@"Okay", nil];
alertView.alertViewStyle = UIAlertViewStylePlainTextInput;
myTextField = [alertView textFieldAtIndex:0];
myTextField.text=mainString;
[alertView show];
[alertView release];
[alertView retain];
}
任何人都可以解释为什么会发生这种情况,以及如何预防? Thanx提前
答案 0 :(得分:1)
试试这个,
- (void)AlertServer:(UILongPressGestureRecognizer*)sender {
if (sender.state == UIGestureRecognizerStateEnded) {
alertView = [[UIAlertView alloc] initWithTitle:@"Server" message:@"" delegate:self cancelButtonTitle:@"Cancel" otherButtonTitles:@"Okay", nil];
alertView.alertViewStyle = UIAlertViewStylePlainTextInput;
myTextField = [alertView textFieldAtIndex:0];
myTextField.text=mainString;
[alertView show];
[alertView release];
}
else if (sender.state == UIGestureRecognizerStateBegan){
NSLog(@"UIGestureRecognizerStateBegan.");
//Do Whatever You want on Began of Gesture
}
}
答案 1 :(得分:0)
根据您的观察结果更改longPressGestureRecognizer.minimumPressDuration
(时间间隔以秒为单位。默认持续时间为0.5秒。)或使用一些标记来检查是否已显示警报。