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.

1

2 Answers 2

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?)

Sign up to request clarification or add additional context in comments.

2 Comments

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.
Where do I put the override function? Which class, and is it before I perform the segue function?
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.

3 Comments

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.
Sorry for the late reply. Later version of Xcode 6 changed the method. I have updated it above and it should work fine now.
if (segue.identifier = "MainToTimer") { needs to be if (segue.identifier == "MainToTimer") { ur comparing, not assigning

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.