20

I use Swift and Xcode 6 and would like to pass a variable from one View Controller to another using a Segue.

I have created a segue called 'MainToTimer' which is trigger once button is pressed. I would like to be able to use the variable called 'Duration' on the second View Controller.

Is it possible to pass multiple variables and constants?

What code do I need associated to each View Controller?

Thank you in advance.

Paulw11
  • 108,386
  • 14
  • 159
  • 186
DanteDeveloper
  • 287
  • 2
  • 5
  • 12
  • 1
    possible duplicate of [How do you pass data between view controllers in Swift?](http://stackoverflow.com/questions/25215476/how-do-you-pass-data-between-view-controllers-in-swift) – Paulw11 Sep 01 '14 at 01:33

2 Answers2

44

First, setup property/properties to hold your variables in your second view controller (destination).

class YourSecondViewController: UIViewController {
    var duration:Double?
}

Then have your button trigger your custom segue. Use your variable ('duration') as the argument for sender.

class YourFirstViewController: UIViewController {
    @IBAction func buttonTapped(sender: AnyObject) {
        self.performSegueWithIdentifier("MainToTimer", sender: duration)
    }
}

Finally, pass this sender data by overriding the prepareForSegue method:

override func prepareForSegue(segue: UIStoryboardSegue!, sender: AnyObject!) {
    if (segue.identifier == "MainToTimer") {
        let secondViewController = segue.destinationViewController as YourSecondViewController
        let duration = sender as Double
        secondViewController.duration = duration
    }
}

Yes, it is also possible to pass multiple variables and constants, again using the 'sender' parameter of prepareForSegue. If you have multiple data you want to pass in, put them in an array and make that array the sender.

SWIFT 3 From Swift 3, the method prepareForSegue(segue: UIStoryboardSegue!, sender: AnyObject!) has changed to prepare(for segue: UIStoryboardSegue, sender: Any?)

aphoe
  • 2,586
  • 1
  • 27
  • 31
Donn
  • 1,680
  • 14
  • 17
  • 1
    OK ... I see how this works ... I might even use it ... but I have a bad feeling since this is not really what the sender argument was designed for. Usually I find out later there why you shouldn't do this, but I can't think of anything right now. – wcochran Oct 05 '17 at 20:02
  • 1
    Where do I put the override function? Which class, and is it before I perform the segue function? – George_E -old Mar 28 '18 at 16:43
6

In the first ViewController place this (for modal segue):

override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
    let theDestination = (segue.destinationViewController as ViewController2)
    theDestination.Duration2 = Duration
}

Change ViewController2 to the name of the second ViewController. In ViewController2 create a class variable:

var Duration2 = (whatever the type - UInt8 I guess for time)

That's it. You will have in the value of Duration2 the value of Duration from the first ViewController.

Carrie Kendall
  • 11,124
  • 5
  • 61
  • 81
Steve Rosenberg
  • 19,348
  • 7
  • 46
  • 53
  • Thank you for your comment. I tried to incorporate your suggestion and have received an error. (1) in line self.performSegueWithIdentifier("MainToTimer", sender Duration) I receive an error saying Type 'Duration' does not conform to protocol 'Any Object' (2)in line theDestination.Duration2 = Duration I receive an error - Expected member name or constructor call after type name. – DanteDeveloper Sep 02 '14 at 18:40
  • Sorry for the late reply. Later version of Xcode 6 changed the method. I have updated it above and it should work fine now. – Steve Rosenberg Sep 21 '14 at 01:19
  • `if (segue.identifier = "MainToTimer") {` needs to be `if (segue.identifier == "MainToTimer") {` ur comparing, not assigning – Pedro Luz Nov 22 '15 at 17:32