Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8279943
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 8, 20262026-06-08T09:29:38+00:00 2026-06-08T09:29:38+00:00

Possible Duplicate: UILongPressGestureRecognizer on UITableViewCell – double call I am new to iPhone, I

  • 0

Possible Duplicate:
UILongPressGestureRecognizer on UITableViewCell – double call

I am new to iPhone,

I am displaying Alert when Button is pressed long, but when i press button long my alert view gets called again.

Here is my code snippet,

- (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];
    }
}

Any help will be appreciated.

EDIT:

  -(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];

      } 

   }
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-08T09:29:41+00:00Added an answer on June 8, 2026 at 9:29 am

    UILongPressGestureRecognizer is a long event process. Please check the event state either it has started, completed etc.

    - (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.");
        }       
    }
    

    I think, better to use the UIGestureRecognizerStateBegan state in your case.

    Please modify your 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;
    
       }
     }
    

    Please check all the below 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
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Possible Duplicate: Can main function call itself in C++? I found this problem very
Possible Duplicate: Objective C for Windows iPhone development on Windows Is there any way
Possible Duplicate: Python : how to append new elements in a list of list?
Possible Duplicate: How to call a JavaScript function from PHP? I have a php
Possible Duplicate: UIButton long press with finger stationary I have created 100 buttons from
Possible Duplicate: Opening url in new tab while i am doing window.open(), my page
Possible Duplicate: git - removing a file from source control (but not from the
Possible Duplicate: Why do I see a double variable initialized to some value like
Possible Duplicate: Convert a long hex string in to int array with sscanf I
Possible Duplicate: Create an empty object in JavaScript with {} or new Object()? When

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.