문제

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