无法转换类型' NSObject - >的值() - >视图控制器'预期的参数类型' AnyObject?'

时间:2016-08-30 03:55:47

标签: swift uipangesturerecognizer

我正在尝试声明手势识别器。当我在函数中声明它时它工作正常但是当我在类级别声明所以它可用于多个函数时我得到了错误。

  

无法转换类型' NSObject - >的值() - >视图控制器'预期参数类型' AnyObject?'

我宣布这样:

let gestureBack = UIPanGestureRecognizer(target: self, action: Selector("wasDraggedBack:"))

1 个答案:

答案 0 :(得分:3)

If you want Gesture object available to multiple function than declare its instance at class level and initialize the object in viewDidLoad like this.

var gestureBack: UIPanGestureRecognizer?

override func viewDidLoad() {
    super.viewDidLoad()
    self.gestureBack = UIPanGestureRecognizer(target: self, action: Selector("wasDraggedBack:"))
}