UIGestureRecognizer导致未捕获的异常错误

时间:2016-11-04 17:52:49

标签: swift swift3 uiswipegesturerecognizer

UIGestureRecognizer导致未捕获的异常错误。它说无法识别的选择器发送到实例。我使用的是Swift 3和Xcode 8。

这是代码。

import UIKit
import UIKit.UIGestureRecognizer

class ViewController: UIViewController {

override func viewDidLoad() {
    super.viewDidLoad()

    print("starting")        

    var swipeDown = UISwipeGestureRecognizer(target: self, action: 
"respondToSwipeGesture:")
    swipeDown.direction = UISwipeGestureRecognizerDirection.down
    self.view.addGestureRecognizer(swipeDown)

}

func respondToSwipeGesture(gesture: UIGestureRecognizer) {

    if let swipeGesture = gesture as? UISwipeGestureRecognizer {

        switch swipeGesture.direction {

        case UISwipeGestureRecognizerDirection.down:

           // go to View Controller 2


            gopage2()       

        default:
            print("does it work? ")
            break
        }
    }
}





func gopage2() {


    let storyboard=UIStoryboard(name: "Main", bundle: nil)
    let viewController = 
storyboard.instantiateViewController(withIdentifier: "ViewController2")    
as! UIViewController
    self.present(viewController, animated: true)  }


override func didReceiveMemoryWarning() {
    super.didReceiveMemoryWarning()
    // Dispose of any resources that can be recreated.
}





}

respondToSwipeGesture需要是公共类吗?

1 个答案:

答案 0 :(得分:0)

只需在参数中添加“忽略此”下划线。

func respondToSwipeGesture(_ gesture: UIGestureRecognizer) {