iphone - UIAlertView 在 iPhone 中被调用两次

标签 iphone ios ipad uigesturerecognizer uialertview

这个问题在这里已经有了答案:




9年前关闭。




Possible Duplicate:
UILongPressGestureRecognizer on UITableViewCell - double call



我是 iPhone 新手,

我在长按按钮时显示警报,但是当我长按按钮时,我的警报 View 被再次调用。

这是我的代码片段,
- (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];

      } 

   }

最佳答案

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()作为
 -(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

关于iphone - UIAlertView 在 iPhone 中被调用两次,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11627747/

相关文章:

iphone - 使 iPhone 特定的应用程序在 iPad 上运行以满足 Apple 要求

iPhone 影音播放器

JavaScript 在 iPad 上的 Safari 中不起作用

ios - 如何在 iOS 7 的 UISplitViewController 的主视图弹出按钮中显示后退箭头?

iphone - 检测多个按钮被单击的最佳方法是什么

ios - UIScrollView 按偏移量滚动

ios - 如何检查用户当前位置是否在位置圈内

ios - 获取错误无法将类型 'NSTaggedPointerString' 的值转换为 'NSArray'

ios - 具有自定义 UTI 的 iCloud 文档选取器

iphone - 应用程序因内存使用率非常低而崩溃 iphone/iPad