17

I have two VCs: VC1 and VC2. In VC1, I have a finish button which I programmatically made and a result array I want to pass to VC2.

I know how to make Segue in Storyboard, but I cannot do that at this moment since the finish button is programmatically made.

If I want to pass result array using segue, is there a way to make the segue programmatically? If this is not possible, should I just present VC2 using presentViewController and set a delegate to pass the result array?

Kahsn
  • 1,045
  • 3
  • 15
  • 25
  • 6
    You could create a segue in interface builder from VC1 to VC2, give it a name, then call `performSegueWithIdentifier` from the action you've got tied to the button. Then, in `prepareForSegue` you'd, put in an `if` statement IDing the segue you're using, grab whatever you're passing from VC1, assign it on VC2 and BAM...you're done. – Adrian Feb 11 '16 at 20:19
  • Adrian, you should post this as an answer so the OP can accept it. This is the correct answer to the OPs question. – Duncan C Feb 11 '16 at 20:42

2 Answers2

29

You can do it like proposed in this answer: InstantiateViewControllerWithIdentifier.

Furthermore I am providing you the code from the linked answer rewritten in Swift because the answer in the link was originally written in Objective-C.

let vc = UIStoryboard(name:"Main", bundle:nil).instantiateViewController(withIdentifier: "identifier") as! SecondViewController

vc.resultsArray = self.resultsArray

EDIT:

Since this answer draws some attention I thought I provide you with another more failsafe way. In the above answer the application will crash if the ViewController with "identifier" is not of type SecondViewController. In Swift you can prevent this crash by using optional binding:

guard let vc = UIStoryboard(name:"Main", bundle:nil).instantiateViewControllerWithIdentifier("identifier") as? SecondViewController else {
    print("Could not instantiate view controller with identifier of type SecondViewController")
    return
}

vc.resultsArray = self.resultsArray
self.navigationController?.pushViewController(vc, animated:true)

This way the ViewController is pushed if it is of type SecondViewController. If can not be casted to SecondViewController a message is printed and the application remains on the current ViewController.

PhillipJacobs
  • 2,337
  • 1
  • 16
  • 32
dehlen
  • 7,325
  • 4
  • 43
  • 71
  • Since the OP is asking about creating segues from a button action, creating a named `UIViewController` to `UIViewController` segue and invoking it with performSegueWithIdentifier, as suggested by @AdrianB, seems like a better solution. – Duncan C Feb 11 '16 at 20:41
  • @DuncanC made a good point but Dehlen's solution is also working :) I learned two different ways I guess. – Kahsn Feb 11 '16 at 22:12
  • Hi, this worked for me, but now when I click on cancel to navigate back, it doesn't work, any idea what might be wrong? – SamAko Oct 12 '16 at 11:50
  • Do you mean "Cancel" or "Back" ? Did you define your own IBAction for the "Cancel" action ? If so how does it look like ? – dehlen Oct 12 '16 at 12:42
8

You can still create the segue in Interface Builder by dragging from VC1 to VC2 - just drag from/to the little yellow circle at the top of the VC. Give this segue a unique name in IB, and in your finish function you can call performSegueWithIdentifier:, pass in the name of your segue, and that's it. In the prepareForSegue method you can find out which segue is being performed by accessing segue.identifier, and if it's the segue in question you can get a pointer to segue.destinationViewController and pass your data on that way.

creeperspeak
  • 5,403
  • 1
  • 17
  • 38