2013-01-24 68 views
1

我在導航欄上的appdelegate文件(不完全在導航欄上)顯示UILabel。它顯示出完美。但沒有觸摸事件正在呼喚它。 Touch僅在狀態欄上工作。請幫忙。提前致謝。觸摸方法不調用appdelegate

這是我的代碼。

CGRect frame=[[UIApplication sharedApplication] statusBarFrame]; 
backgroundImageView=[[UILabel alloc] initWithFrame:CGRectMake(0, frame.size.height, 320, 44)];   
backgroundImageView.tag=50; 
[backgroundImageView setTextAlignment:UITextAlignmentCenter]; 
[backgroundImageView setBackgroundColor:[UIColor colorWithPatternImage:[UIImage imageNamed:@"music.png"]]]; 
[self.window addSubview:backgroundImageView]; 

- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event 
{ 
    NSLog(@"in touched"); 
} 
+0

u初始化根視圖?????? – iPatel

回答

0

正如泰勒指出在his answer here

有一個UIWindow在一個方便的包羅萬象的方法叫的SendEvent:這 看到事件處理管道的起點附近的每一個事件。如果你 想做任何非標準的附加事件處理,這是一個很好的地方放它。

攔截所有觸摸你應該使用UIView子類爲您的控制器視圖,並覆蓋則hitTest:withEvent:方法方法是:

- (UIView *)hitTest:(CGPoint)point withEvent:(UIEvent *)event { 
     touchedView = [super hitTest:point withEvent:event]; 
     NSSet* touches = [event allTouches]; 
     // handle touches if you need 
     return touchedView; 
     } 

OR

2)你可以使用:

UITapGestureRecognizer *tgr = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(anyMethod)]; 
[imageView addGestureRecognizer:tgr]; 
[tgr release]; 

Before doing this, enable user interaction of UIimageView. 
+0

如何在appdelegate文件中實現這個? – shivam

+0

你的意思是你想啓用即使在ImageView的權利? –

+0

我在appdelegate通知方法的視圖上顯示了一個uilabel。現在我想要一個可點擊的方法。我怎麼能夠? – shivam

相關問題