简体   繁体   English

UILongPressGestureRecognizer在文本字段上显示警报

[英]UILongPressGestureRecognizer Show Alert On Textfield

I have a UILongPressGestureRecognizer added to a UITextField. 我有一个UILongPressGestureRecognizer添加到UITextField。 When I press the UITextField it is show me alert but that is three alert show me. 当我按下UITextField时,它向我显示警报,但这是三个警报向我显示。 That is my code: 那是我的代码:

- (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];
}

Can anyone explain why this happens, and how it can be prevented? 谁能解释这种情况的发生以及如何预防? Thanx in advance 提前感谢

Try this, 尝试这个,

-  (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
     }
  }

根据观察结果更改longPressGestureRecognizer.minimumPressDuration (时间间隔以秒为单位。默认持续时间为0.5秒。)或使用某些标志来检查是否已显示警报。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM