-1

我有一個UIViewController,它包含一個UIScrollView,它的contentview內有一個UIView。爲什麼我的UIViewController不響應觸摸?

我有下面的代碼不能正常工作,鍵盤沒有被駁回,爲什麼?:

#pragma mark - 
#pragma mark Touch Events 
-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event { 

    UITouch *touch = [touches anyObject]; 

    if ([touch view] == scrollView || [touch view] == self.view) 
    { 
     [usernameTextField resignFirstResponder]; 
     [passwordTextField resignFirstResponder]; 
    } 
} 
+0

你是否將textfield委託設置爲self? –

回答

1

touchesBegan:withEvent:UIView方法,而不是一個UIViewController方法。你想在這裏做什麼?你應該很少有對touchesBegan:的UI反應。您可能的意思是改用UITapGestureRecognizer

請確保使用訪問器(self.scrollView),而不是直接訪問您的ivars。直接ivar訪問是內存管理崩潰的頭號原因。

+0

另外,請注意像'touchedBegan:withEvent:','UITapGestureRecognizer's被添加到'UIView',而不是'UIViewController'。雖然他們可以並且應該向控制器發送行動。 – jemmons

+0

UIViewController是UIResponder的子類,因此它可以捕獲輸入事件! – JustSid