滑动左手势swift时无法识别的选择器错误

时间:2017-06-07 23:09:39

标签: swift unrecognized-selector

我正在尝试创建一个简单的swift left segue到下一个视图控制器,但是我收到一个无法识别的选择器错误。这个代码失败了:

   override func viewDidLoad() {
        super.viewDidLoad()

        //below creates the instances for swiping to change screens
        //only added right here because it's the main screen

          var swipeLeft : UISwipeGestureRecognizer = UISwipeGestureRecognizer(target: self, action: Selector("swipe:"))
        swipeLeft.direction = UISwipeGestureRecognizerDirection.Left

        self.view.addGestureRecognizer(swipeLeft)

        func swipe(Sender: UISwipeGestureRecognizer!) {
            print("swiped left")
            let vc = self.storyboard!.instantiateViewControllerWithIdentifier("OnDeck") as! OnDeck
            self.presentViewController(vc, animated: true, completion: nil)
        }
}

这是我得到的错误:

2017-06-07 19:07:00.990 Test[65722:3911717] -[Test.GameView swipe:]: unrecognized selector sent to instance 0x7fc2d310f700
2017-06-07 19:07:01.000 Test[65722:3911717] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[Test.GameView swipe:]: unrecognized selector sent to instance 0x7fc2d310f700'

2 个答案:

答案 0 :(得分:0)

Swift 3 的情况下,选择器应如下所示:

#selector(swipe(Sender:))

您可以使用#selector表示法进行编译时检查。

对于 Swift 2 ,选择器将是:

#selector(swipe(_:))

答案 1 :(得分:0)

你需要这样尝试:

let swiftLeft : UISwipeGestureRecognizer = UISwipeGestureRecognizer(target: self, action: #selector(self.swipe(Sender:)))
swiftLeft.direction = .left
self.view.addGestureRecognizer(swiftLeft)

func swipe(Sender: UISwipeGestureRecognizer!) {
     print("swiped left")

}

如果您正在使用Swift 2,则需要添加选择器

选择器(self.swipe(_:))