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