2017-01-02 63 views
1

我希望能夠在我的ViewController中向右滑動,這將顯示另一個視圖控制器,CommunitiesViewController如何向右滑動以在Swift 3中顯示新的View Controller?

我已經看過其他線程上,發現這樣做的某些方面,但我相信他們是雨燕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: terminating with uncaught exception of type NSException

回答

3

錯誤選擇格式,更改爲:

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

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

試穿:

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 
     } 
    } 
} 
相關問題