Ios 如何在Swift 3中向右滑动以显示新的视图控制器?

Ios 如何在Swift 3中向右滑动以显示新的视图控制器?,ios,swift,xcode,uigesturerecognizer,Ios,Swift,Xcode,Uigesturerecognizer,我希望能够在我的ViewController中向右滑动,这将显示另一个视图控制器,communities ViewController 我已经研究了其他线程,并找到了一些这样做的方法,尽管我相信它们适用于Swift 2 这是我在视图控制器中使用的代码: override func viewDidLoad() { super.viewDidLoad() let swipeRight = UISwipeGestureRecognizer(target: self, action:

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

我已经研究了其他线程,并找到了一些这样做的方法,尽管我相信它们适用于Swift 2

这是我在视图控制器中使用的代码:

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
一个
CommunitiesID
的故事板ID

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

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


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

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

尝试:

override func viewDidLoad() {
    super.viewDidLoad()
    // Do any additional setup after loading the view, typically from a nib.

    // Gesture Recognizer     
    let swipeRight = UISwipeGestureRecognizer(target: self, action: #selector(self.respondToSwipeGesture))
    swipeRight.direction = UISwipeGestureRecognizerDirection.right

    self.view.addGestureRecognizer(swipeRight)
    let swipeLeft = UISwipeGestureRecognizer(target: self, action: #selector(self.respondToSwipeGesture))
    swipeLeft.direction = UISwipeGestureRecognizerDirection.left
    self.view.addGestureRecognizer(swipeLeft)

}
然后添加函数:

func respondToSwipeGesture(gesture: UIGestureRecognizer) {
    if let swipeGesture = gesture as? UISwipeGestureRecognizer {
        switch swipeGesture.direction {
        case UISwipeGestureRecognizerDirection.right:
            //right view controller
            let newViewController = firstViewController() 
            self.navigationController?.pushViewController(newViewController, animated: true)
        case UISwipeGestureRecognizerDirection.left:
            //left view controller
            let newViewController = secondViewController() 
            self.navigationController?.pushViewController(newViewController, animated: true)
        default:
            break
        }
    }
}

很好的工作,但在控制器插入表视图或滚动视图中,滑动“无工作完美”时出现问题:(
func respondToSwipeGesture(gesture: UIGestureRecognizer) {
    if let swipeGesture = gesture as? UISwipeGestureRecognizer {
        switch swipeGesture.direction {
        case UISwipeGestureRecognizerDirection.right:
            //right view controller
            let newViewController = firstViewController() 
            self.navigationController?.pushViewController(newViewController, animated: true)
        case UISwipeGestureRecognizerDirection.left:
            //left view controller
            let newViewController = secondViewController() 
            self.navigationController?.pushViewController(newViewController, animated: true)
        default:
            break
        }
    }
}