Why do I have to dismiss this UIAlertView three times?

后端 未结 2 1832
名媛妹妹
名媛妹妹 2021-01-24 03:19

I am showing an alert on UILongPressGestureRecognizer, the issue I am facing is that every time I have to click it three times to dismiss the alert while the alert should be dis

2条回答
  •  一向
    一向 (楼主)
    2021-01-24 03:46

    It fires multiple times to indicate the different states of the gesture (began, changed, ended, etc). So in the handler method, check the state property of the gesture recognizer to avoid doing the action at each state of the gesture.

    In your case:

    - (void)tableCellPressed:(UILongPressGestureRecognizer *)recognizer 
    {
    VehicleListCell* cell = (VehicleListCell *)[recognizer view];
    cellValueForLongPress = cell.licPlate.text;
    
    NSLog(@"cell value: %@", cellValueForLongPress);
    
    
    if (gestureRecognizer.state == UIGestureRecognizerStateBegan)
    {
    UIAlertView *alert = [[UIAlertView alloc] initWithTitle:nil message:nil delegate:self cancelButtonTitle:@"Cancel" otherButtonTitles: nil] ;
    
    [alert addButtonWithTitle:@"Add to Favourites"];
    [alert addButtonWithTitle:@"Take to Map"];
    
    [alert show];
    }
    }
    

提交回复
热议问题