Why do I have to dismiss this UIAlertView three times?

后端 未结 2 1829
名媛妹妹
名媛妹妹 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];
    }
    }
    
    0 讨论(0)
  • 2021-01-24 04:07

    The issue is that your long press gesture recogniser fires for each of the states, began, ended etc. If the state of the event is not the 'began' one then you should return and not perform the subsequent code.

    - (void)tableCellPressed:(UILongPressGestureRecognizer *)recognizer { 
           if (recognizer.state != UIGestureRecognizerStateBegan) {
                return;
            }
    
           // ... rest of code
    }
    
    0 讨论(0)
提交回复
热议问题