swift 2 TapGestureRecognizer不接受自己作为目标

时间:2015-11-20 16:29:09

标签: ios swift swift2 uitapgesturerecognizer

我正在开发一个项目然后我需要添加一个tapGesture,问题是我无法将self添加为目标,我不断收到此错误:

Cannot convert value of type 'NSObject -> () -> ViewController' to expected argument type 'AnyObject?'

这是我的代码:

我也试过了:

let tap = UITapGestureRecognizer(target: self, action: "dismissKeyboard")

但是我得到了同样的错误

1 个答案:

答案 0 :(得分:3)

将该行放在onBackPressed而不是班级:

viewDidLoad

根据我在您编辑问题时看到的内容,您的override func viewDidLoad() { super.viewDidLoad() // Do any additional setup after loading the view, typically from a nib. let tap = UITapGestureRecognizer(target: self, action: "dismissKeyboard") } 变量超出了范围。