用于向下滑动键盘错误的UISwipeGestureRecognizer

时间:2015-03-21 21:14:13

标签: ios swift

我有:

    var swipe: UISwipeGestureRecognizer = UISwipeGestureRecognizer(target: self, action: "dismissKeyboard")
    swipe.direction = UISwipeGestureRecognizerDirection.Down
    self.view.addGestureRecognizer(swipe)

override func didReceiveMemoryWarning() {
    super.didReceiveMemoryWarning()

    func dismissKeyboard() {
        self.messageTextView.resignFirstResponder()
    }

}

当我向下滑动设备时,我收到错误:

*** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[AppName.ViewController dismissKeyboard]: unrecognized selector sent to instance 0x125d06500'

我的viewDidLoad中有很多其他代码,而我的messageTextView位于UITiew中,位于UITableView中。可能是什么问题呢?

1 个答案:

答案 0 :(得分:0)

您的dismissKeyboard方法嵌套在didReceiveMemoryWarning方法中。这是错的。您必须将dismissKeyboard方法声明为视图控制器的方法。然后它应该工作。

顺便说一下。您说您的messageTextView是否嵌入在表格视图中? UITableView类已经提供了在滑动时关闭键盘的机会。你只需要像这样设置dismissMode

tableView.keyboardDismissMode = UIScrollViewKeyboardDismissModeInteractive;

结帐Documentation for UIScrollViewKeyboardDismissMode