2012-07-24 24 views
2

我在使用UITapGestureRecognizer和UIButton的iPhone applcation中工作。我在self.view中添加了UITapGestureRecognizer。當我點擊UIButton時,它不會調用它的動作,只能調用tapiseure動作。如何解決這個問題?任何人都可以請幫忙解決這個問題嗎?UIButton操作不使用UITapGestureRecognizer iphone應用程序調用?

這是我在-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. 
} 

請幫我解決這個問題。提前致謝。

回答

3

爲什麼沒有人使用堆棧溢出或Google的精彩搜索?

這裏是你的答案:https://stackoverflow.com/a/3348532/312312

而且在同一個線程,這似乎是連一個簡單的解決方案: https://stackoverflow.com/a/8891030/312312

+0

謝謝。我在我的代碼中嘗試了這些項目。但我忘了提到delgate = self。這就是爲什麼我得到了答案。謝謝你的幫助。 – Gopinath 2012-07-24 13:49:55

+0

您使用了哪兩種解決方案?第二種方法似乎更簡單 – Lefteris 2012-07-24 13:51:14

+0

第二個答案我已經用來解決這個問題。非常感謝。 – Gopinath 2012-07-24 13:54:50

0

這是最好的解決辦法: 你宣佈你的tapRecognizer後(I」米指的是你的代碼的問題)增加以下內容:

tapRecognizer.cancelsTouchesInView = NO;