2012-01-07 22 views
1

我有一個子視圖(UIImageView)添加到我的主視圖。我只想在子視圖中檢測點擊,然後運行'processTap'方法。Gesturerecognizer上的子視圖,獲取錯誤

當它檢測到點擊時,我收到以下異常。

「NSInvalidArgumentException '的,理由是:' - [UIImageView的processTap]: 無法識別的選擇發送到實例」

東西似乎是錯誤的@selector部分。

任何想法?

- (void)viewDidLoad 
{ 
    // Create a uiimage view, load image 
    UIImageView *tapView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"tap zone.png"]]; 
    tapView.frame = CGRectMake(20, 50, 279, 298); 
    tapView.userInteractionEnabled = YES; 

    [self.view addSubview:tapView]; 

    // Initialize tap gesture recognizers 
    UITapGestureRecognizer *singleTap = [[UITapGestureRecognizer alloc] 
            initWithTarget:tapView 
            action:@selector(processTap)]; 
    singleTap.numberOfTapsRequired = 1; 

    [tapView addGestureRecognizer:singleTap]; 

    [super viewDidLoad]; 
} 

- (void)processTap { 
    if (sessionRunning) { 
     tapCounter = tapCounter + 1; 
     _tapCount.text = [NSString stringWithFormat:@"%i",tapCounter]; 
    } 
}' 

回答

3

您的識別器的目標必須是self

// Initialize tap gesture recognizers 
UITapGestureRecognizer *singleTap = [[UITapGestureRecognizer alloc] 
            initWithTarget:self 
            action:@selector(processTap)]; 

你有以前的錯誤,因爲你的tapView還沒有選擇叫processTap

注意

從蘋果公司的文檔:

目標

一個對象,它是在識別手勢接收器發送動作消息的接收方。零不是有效的值。

因此,在這種情況下,您的操作的收件人是實施processTap選擇器的控制器。