2

Am working in iPhone applcation using UITapGestureRecognizer and UIButton. I have added UITapGestureRecognizer in self.view. When i click on the UIButton it won't call it's action the tapgesture action only calling. How to solve this issure? Can anyone please help to solve this issue?

Here is my code in -ViewDidLoad,

UITapGestureRecognizer *tapRecognizer = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(handleTapFrom:)];
    [self.view addGestureRecognizer:tapRecognizer];
    [tapRecognizer release];

UIButton *infoButton1 = [UIButton buttonWithType:UIButtonTypeInfoDark];
    infoButton1.frame = CGRectMake(0, 5, 30, 30);
    infoButton1.tag = 1;
    infoButton1.opaque = YES;
    [infoButton1 addTarget:self action:@selector(infoAlert:) forControlEvents:UIControlEventTouchUpInside | UIControlEventTouchUpOutside];
    [toolBarView1 addSubview:infoButton1]; 

-(void) infoAlert:(id) sender
{
    NSLog(@"Get Info Alert"); // Here the control not calling..
}


-(void)handleTapFrom:(UITapGestureRecognizer *)recognizer
{    
    NSLog(@"%@",[NSString stringWithFormat:@"%d", recognizer.view.tag]); // Whenever i touch the button this method only calling.
}

Please help me to solve this issue. Thanks in advance.

Gopinath
  • 5,392
  • 21
  • 64
  • 97

2 Answers2

3

Why is no one using the wonderful search of stack overflow or Google ?

Here is your answer: https://stackoverflow.com/a/3348532/312312

And from the same thread, this seems to be even a simpler solution: https://stackoverflow.com/a/8891030/312312

Community
  • 1
  • 1
Lefteris
  • 14,550
  • 2
  • 56
  • 95
  • Thank you. I have tried these items in my code. But i have forgot to mentioned the delgate = self. That's why i got get the answer. Thanks for your help. – Gopinath Jul 24 '12 at 13:49
  • Which of the 2 solutions did you use? The second approach seems simpler – Lefteris Jul 24 '12 at 13:51
  • second answer i have used to solve this problem. Thank you so much. – Gopinath Jul 24 '12 at 13:54
  • I have used search and got here. So I guess it works... thanks for the tip, it seems like gesture recognizers always have one more little surprise up the sleeve. – n13 Feb 09 '13 at 04:27
0

this is the best solution: after you declared your tapRecognizer (i'm referring to your code on the question) add the following:

  tapRecognizer.cancelsTouchesInView = NO;
Gaucho
  • 1,328
  • 1
  • 17
  • 32