6

I'm trying to segue to a view controller. the segue's identifier is "Orange" (the other view controller is just an orange view). The view controller is segued through storyboard and connected to an image view in my main view controller. I want to trigger this with a pan gesture recognizer. So when a pan is detected, it just segues to the view controller.

This is my attempt:

@IBOutlet var panGesture: UIPanGestureRecognizer!

@IBAction func onPan(sender: UIPanGestureRecognizer) {
    if panGesture.state == .Ended {
        print("panned")
    performSegueWithIdentifier("Orange", sender: sender)
    }
}

However, this is not working. I know it does detect the pan, but it doesn't perform the segue.

I read how to present the segue from IOS - How to segue programmatically using swift

Community
  • 1
  • 1
Useful_Investigator
  • 938
  • 2
  • 8
  • 27

3 Answers3

4

Try presenting ViewController's by instantiation: -

  • Make sure you have a navigation Controller embed in your First VC

  • Giving your ViewController a StoryBoardID :- Select the ViewController in your storyboard which you want to present -> Identity Inspector -> StoryBoard ID Give your VC a storyboard ID lets say : "vc2SceneVC_ID"

  • Instantiate and push the VC like this:-

    @IBAction func onPan(sender: UIPanGestureRecognizer) {
    
    if panGesture.state == .Ended {
    print("panned")
    
     let vc2Scene = self.storyboard?.instantiateViewControllerWithIdentifier("vc2SceneVC_ID") as! VC2
     self.navigationController?.pushViewController(vc2Scene, animated: true)
    
    }
    

Apple Documentation for Navigation Controller :- UINavigationController Apple Doc

PS:- Prefer instantiating the navigating through ViewControllers with instantiation rather performSegueWithIdentifier("Orange", sender: sender) Because this way you get to transfer data back-forth those viewControllers, also it prevents Memory Leaks in most cases.

Dravidian
  • 9,945
  • 3
  • 34
  • 74
2

Step 1: Control+Click from the first ViewController (where is the View Controller lab is shown) and drag to the second ViewController

step 2 :select the type of segue

step 3:select the segue and set the identifier

and where you need to perform the segue just call self.performSegueWithIdentifier("test", sender: nil)

sorry for bad English

Abdul Rehman
  • 2,386
  • 1
  • 20
  • 34
1

Orange must be the identifier of your segue in the storyboard and not the view controller identifier.

If your segue identifier is set properly try testing if your gesture callback is in the main thread.

Let me know in the comments you have problems.

Marco Santarossa
  • 4,058
  • 1
  • 29
  • 49