1

重複の可能性:
UITableViewCell の UILongPressGestureRecognizer - 二重呼び出し

iPhone初心者ですが、

ボタンを長押しするとアラートを表示していますが、ボタンを長押しするとアラートビューが再度呼び出されます。

これが私のコードスニペットです。

- (IBAction)longPressDetected:(UIGestureRecognizer *)gestureRecognizer {

    //Gets text of Button.
    UIButton *btn = (UIButton *)[gestureRecognizer view];
    BtnText= [btn titleForState:UIControlStateNormal];
    NSLog(@"longPressDetected");

    UIAlertView* alert_view = [[UIAlertView alloc]
                               initWithTitle: @"Are you sure want to Delete ?" message:nil delegate: self 
                               cancelButtonTitle: @"Yes" otherButtonTitles: @"No", nil];
    [alert_view show];
    [alert_view release];
}

- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
{ 
    if (buttonIndex==0) {

        [self ReloadView];
         [alertView dismissWithClickedButtonIndex:0 animated:TRUE];
    }
    else{
        [alertView dismissWithClickedButtonIndex:1 animated:TRUE];
    }
}

どんな助けでも大歓迎です。

編集:

  -(void)viewWillAppear:(BOOL)animated
   {
         for(int i=0;i<10i++)
            {

                if(i!=0)
                {
                    if (i%4==0) 
                    {                    
                        ypos+=180;
                        xpos=30;
                    }
                    else
                    {
                        xpos+=200;
                    }
                }


                button = [UIButton buttonWithType:UIButtonTypeCustom];
                button.frame = CGRectMake(xpos, ypos, 120,130);
                [button setBackgroundImage:[UIImage imageNamed:@"ibook2.png"] forState:UIControlStateNormal];
                [button setTitle:[NSString stringWithFormat:@"%@", [Downloadedepubs objectAtIndex:i]] forState:UIControlStateNormal];
                [button addTarget:self action:@selector(buttonClicked:) forControlEvents:UIControlEventTouchUpInside];

                 LongPress = [[UILongPressGestureRecognizer alloc] init];
                [LongPress addTarget:self action:@selector(longPressDetected:)];
                 LongPress.delegate = (id<UIGestureRecognizerDelegate>)self;
                [button addGestureRecognizer:LongPress];
                [self.view addSubview:button];
                [LongPress release];

      } 

   }
4

2 に答える 2

6

UILongPressGestureRecognizer長いイベントプロセスです。開始、完了などのイベントの状態を確認してください。

- (IBAction)longPressDetected:(UIGestureRecognizer *)gestureRecognizer {        

    if (gestureRecognizer.state == UIGestureRecognizerStateBegan){

        NSLog(@"Long press began");

    } else if ( gestureRecognizer.state == UIGestureRecognizerStateRecognized ) {

            NSLog(@"Long press UIGestureRecognizerStateRecognized");
    }
    else if (gestureRecognizer.state == UIGestureRecognizerStateEnded) {

        NSLog(@"Long press Ended");
    }
    else {

        NSLog(@"Long press detected.");
    }       
}

あなたの場合はUIGestureRecognizerStateBegan状態を使用する方が良いと思います。

longPressDetected()as を変更してください

 -(IBAction)longPressDetected:(UIGestureRecognizer *)gestureRecognizer {

   if (gestureRecognizer.state == UIGestureRecognizerStateBegan){

       UIButton *btn = (UIButton *)[gestureRecognizer view];
       BtnText= [btn titleForState:UIControlStateNormal];
       NSLog(@"longPressDetected");

      UIAlertView* alert_view = [[UIAlertView alloc] initWithTitle: @"Are you sure want to Delete ?" message:nil delegate: self cancelButtonTitle: @"Yes" otherButtonTitles: @"No", nil];
      [alert_view show];
      [alert_view release];
      alert_view = nil;

   }
 }

以下のすべてのUIGestureRecognizerStatesを確認してください。

    UIGestureRecognizerStatePossible,   // the recognizer has not yet recognized its gesture, but may be evaluating touch events. this is the default state

    UIGestureRecognizerStateBegan,      // the recognizer has received touches recognized as the gesture. the action method will be called at the next turn of the run loop
    UIGestureRecognizerStateChanged,    // the recognizer has received touches recognized as a change to the gesture. the action method will be called at the next turn of the run loop
    UIGestureRecognizerStateEnded,      // the recognizer has received touches recognized as the end of the gesture. the action method will be called at the next turn of the run loop and the recognizer will be reset to UIGestureRecognizerStatePossible
    UIGestureRecognizerStateCancelled,  // the recognizer has received touches resulting in the cancellation of the gesture. the action method will be called at the next turn of the run loop. the recognizer will be reset to UIGestureRecognizerStatePossible

    UIGestureRecognizerStateFailed,     // the recognizer has received a touch sequence that can not be recognized as the gesture. the action method will not be called and the recognizer will be reset to UIGestureRecognizerStatePossible

    // Discrete Gestures – gesture recognizers that recognize a discrete event but do not report changes (for example, a tap) do not transition through the Began and Changed states and can not fail or be cancelled
    UIGestureRecognizerStateRecognized = UIGestureRecognizerStateEnded // the recognizer has received touches recognized as the gesture. the action method will be called at the next turn of the run loop and the recognizer will be reset to UIGestureRecognizerStatePossible
于 2012-07-24T10:01:28.617 に答える
0

UILongPressGestureRecognizer の詳細を入力しなくても、これを簡単に解決できます。を定義しBOOL isShowingAlertViewます。を呼び出すとき[alertView show]は を設定isShowingAlertView = YESし、閉じるときはを設定しますisShowingAlertView = NO。そして、呼び出す前に、チェックするだけです:if (isShowingAlertView == NO)そして、それを表示します:

-(void)viewDidLoad
{
    [super viewDidLoad];
    isShowingAlert = NO;
}

- (IBAction)longPressDetected:(UIGestureRecognizer *)gestureRecognizer {

    if (isShowingAlert == NO)
    {
        isShowingAlert = YES;
        //Gets text of Button.
        UIButton *btn = (UIButton *)[gestureRecognizer view];
        BtnText= [btn titleForState:UIControlStateNormal];
        NSLog(@"longPressDetected");

        UIAlertView* alert_view = [[UIAlertView alloc]
                               initWithTitle: @"Are you sure want to Delete ?" message:nil delegate: self 
                               cancelButtonTitle: @"Yes" otherButtonTitles: @"No", nil];
        [alert_view show];
        [alert_view release];
    }
}

- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
{
    isShowingAlert = NO;
    if (buttonIndex==0)
    {
        [self ReloadView];
    }
}
于 2012-07-24T09:27:33.960 に答える