I have a UILongPressGestureRecognizer added to a UITextField. When I press the UITextField it is show me alert but that is three alert show me. 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
     }
  }

其他提示

Change your longPressGestureRecognizer.minimumPressDuration based on your observations (The time interval is in seconds. The default duration is is 0.5 seconds.) or use some flag to check if Alert is already shown.

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top