我希望能够在ViewController中向右滑动,这将显示另一个视图控制器CommunitiesViewController

我查看了其他线程,发现了一些实现此目的的方法,尽管我相信它们适用于Swift 2。

这是我在ViewController中使用的代码:

override func viewDidLoad() {
    super.viewDidLoad()

    let swipeRight = UISwipeGestureRecognizer(target: self, action: Selector(("respondToSwipeGesture")))
    swipeRight.direction = UISwipeGestureRecognizerDirection.right
    self.view.addGestureRecognizer(swipeRight)
}

  func respondToSwipeGesture(gesture: UIGestureRecognizer) {

    print ("Swiped right")

    if let swipeGesture = gesture as? UISwipeGestureRecognizer {

        switch swipeGesture.direction {

        case UISwipeGestureRecognizerDirection.right:


            //change view controllers

            let storyBoard : UIStoryboard = UIStoryboard(name: "Main", bundle:nil)

            let resultViewController = storyBoard.instantiateViewController(withIdentifier: "CommunitiesID") as! CommunitiesViewController

            self.present(resultViewController, animated:true, completion:nil)


        default:
            break
        }
    }
}

我给了CommunitiesViewController一个故事板ID CommunitiesID

但这不起作用,当我向右滑动时出现以下错误,应用程序崩溃:

libc ++ abi.dylib:以NSException类型的未捕获异常终止

最佳答案

选择器格式错误,请更改为:

action: #selector(respondToSwipeGesture)
func respondToSwipeGesture(gesture: UIGestureRecognizer)

要么
action: #selector(respondToSwipeGesture(_:))
func respondToSwipeGesture(_ gesture: UIGestureRecognizer)

07-28 03:02