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 7082983
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T07:07:20+00:00 2026-05-28T07:07:20+00:00

I am showing an alert on UILongPressGestureRecognizer, the issue I am facing is that

  • 0

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 dismissed on single click on button.

And due to this abnormal behavior, entries get duplicated in the core data..

The code I am using is as under:

In cellForRowAtIndexPath:

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    // NSString * cellValue;
    if (tableView == listTable) {
       cellValue = [listVehicles objectAtIndex:indexPath.row];
    } else { // handle search results table view
        cellValue = [filteredListItems objectAtIndex:indexPath.row];
    }

    static NSString *CellIdentifier = @"vlCell";

    VehicleListCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];

    if (cell == nil) {
        NSLog(@"Cell Created");

        NSArray *nibObjects = [[NSBundle mainBundle] loadNibNamed:@"VehicleListCell" owner:nil options:nil];

        for (id currentObject in nibObjects) {
            if ([currentObject isKindOfClass:[VehicleListCell class]]) {
                cell = (VehicleListCell *)currentObject;
            }
        }

        UILongPressGestureRecognizer *pressRecongnizer = [[UILongPressGestureRecognizer alloc] initWithTarget:self action:@selector(tableCellPressed:)];
        pressRecongnizer.minimumPressDuration = 0.5f;
        [cell addGestureRecognizer:pressRecongnizer];
        [pressRecongnizer release];
    }

    cell.textLabel.font = [UIFont systemFontOfSize:10];

    [[cell ignition] setImage:[UIImage imageNamed:@"ignition.png"]];
    [[cell direction] setImage:[UIImage imageNamed:@"south.png"]];

    cell.licPlate.text = cellValue;

    NSLog(@"cellvalue for cellforRow: %@", cell.licPlate.text);

    return cell;
}

In (void)tableCellPressed:(UILongPressGestureRecognizer *)recognizer method

- (void)tableCellPressed:(UILongPressGestureRecognizer *)recognizer {
    VehicleListCell* cell = (VehicleListCell *)[recognizer view];
    cellValueForLongPress = cell.licPlate.text;

    NSLog(@"cell value: %@", cellValueForLongPress);

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

In alert view method:

-(void)alertView:(UIAlertView *)alert clickedButtonAtIndex:(NSInteger)buttonIndex {

    NSString *title = [alert buttonTitleAtIndex:buttonIndex];

    NSManagedObjectContext *context = [app managedObjectContext];
    Favouritesdata * favourites = [NSEntityDescription insertNewObjectForEntityForName:@"Favouritesdata" inManagedObjectContext:context];

    if([title isEqualToString:@"Add to Favourites"])
    {
        NSLog(@"Added to favourites.");

        NSLog(@"cellValueForLongPress: %@", cellValueForLongPress);


        if (cellValueForLongPress <= 0) {
            NSLog(@"There is no value to save");
        }
        else {
            favourites.licenseplate = cellValueForLongPress;
        }

        [alert dismissWithClickedButtonIndex:0 animated:YES];
    }
    else if([title isEqualToString:@"Take to Map"])
    {
        NSLog(@"Go to MapView");
    }

    NSError *error;

    if (![context save:&error]) {
        NSLog(@"Error Occured");} 
}

What can be the issue?

  • 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-05-28T07:07:21+00:00Added an answer on May 28, 2026 at 7:07 am

    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
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have problem with if condition. I write a script who should showing alert
I have an application where I am showing an alert message using UIAlertView. By
I have a question about showing an alert on the screen. The thing is:
In my application ,i having 4 UIbuttons .On click each button showing same alert
I am creating a web user control for showing Alert on AspxGridView Columns(Delete/Edit) click
Here is my issue with IE.I am showing an success message in alert after
Possible Duplicate: Prevent Back button from showing POST confirmation alert I have used the
I am showing alert box in my page, but after that my page is
I am showing an alert box in my app to enable GPS, app works
I want to display script errors in a popup alert instead of showing them

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.