2
我想初始化一个UIPanGestureRecognizer
作为UIViewController
的属性定义的一部分,这样我就不必声明它是可选的(因为如果初始化仅在viewDidLoad
中发生,我将不得不声明它是可选的)。将UIGestureRecognizer初始化为Swift中属性定义的一部分?
以下两次尝试都失败在编译的时候(我使用的是最新版本的Xcode):
-- 1st attempt
class TestController: UIViewController {
let panGestureRecognizer: UIPanGestureRecognizer
required init(coder: NSCoder) {
super.init(coder: coder)
panGestureRecognizer = UIPanGestureRecognizer( target: self, action: "handlePan:")
// fails with "Property 'self.panGestureRecognizer' not initialized at super.init call' or
// fails with "'self' used before super.init call'
// depending on the order of the two previous statements
}
}
-- 2st attempt
class TestController: UIViewController {
let panGestureRecognizer = UIPanGestureRecognizer(target:self, action: "handlePan:")
// fails with "Type 'TestController ->() -> TestController!' does not conform to protocol 'AnyObject'
}
有另一种有效的方法,从而完成这项工作?
Thx。创建手势识别器,调用超级初始化,然后添加自己作为目标确实有效。 – Drux 2014-11-23 11:47:05