Question

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

Was it helpful?

Solution

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

OTHER TIPS

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.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top