Frage

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

War es hilfreich?

Lösung

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

Andere Tipps

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.

Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit StackOverflow
scroll top