tags:

views:

163

answers:

3

I've set up a UITapGestureRecognizer on viewDidLoad of my view controller but somehow it fires the selector method twice for a single tap.

UITapGestureRecognizer *g = [[[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(openInMapsApp:)] autorelease];
[self.mapView addGestureRecognizer:g];

My method:

-(void)openInMapsApp:(UIGestureRecognizer*)g {
UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:@""
                                                    message:@"This will open this location in the Maps application. Continue?"
                                                   delegate:self
                                          cancelButtonTitle:@"Cancel"
                                          otherButtonTitles:@"OK",nil];
[alertView show];
[alertView release];
}
+1  A: 

I'm seeing this same issue. Anyone figure out the cause yet?

Edit: As a workaround for now, I added a timer to the view that checks to make sure the touch was at least half a second ago, this way the second touch is ignored. Would still like to fix the real problem.

Alavoil
+2  A: 

I can confirm the same. I submitted a bug report to Apple with a sample project demonstrating the issue.

The temporary workaround I've found is to disable the UITapGestureRecognizer immediately before showing the Alert. Then, in the UIAlertView delegate method(s) you implement, re-enable it. This requires you to keep track of the GR somehow, but it seems like the most elegant solution for the time-being.

Using the sample code above:

-(void)openInMapsApp:(UIGestureRecognizer*)g {
    UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:@""
                                                message:@"This will open this location in the Maps application. Continue?"
                                               delegate:self
                                      cancelButtonTitle:@"Cancel"
                                      otherButtonTitles:@"OK",nil];
    g.enabled = NO;
    self.activeGestureRecognizer = g;
    [alertView show];
    [alertView release];
}

- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex {
    self.activeGestureRecognizer.enabled = YES;
    self.activeGestureRecognizer = nil;
}

- (void)alertViewCancel:(UIAlertView *)alertView {
    self.activeGestureRecognizer.enabled = YES;
    self.activeGestureRecognizer = nil;
}
gorbster
This solution worked for me as well. Is this in fact a bug?
mvexel
I had the same issue an an action sheet. This workaround fixed it. Thanks!
Mark Struzinski
A: 

I had the same problem, and I noticed the second event had the state UIGestureRecognizerStateCancelled (whereas the first was UIGestureRecognizerStateEnded), so another workaround is to ignore the event in that case.

Adam Crume