How to pass data between ViewControllers in Swift

How to pass data between ViewControllers in Swift

**UPDATE** This post is a couple years old now, I am working on an update ASAP to make this compatible with Swift 3!

A question I have been asked a lot by people relatively new to iOS development is “how do I pass data/variables between ViewControllers in Swift?”, this short tutorial aims to show you exactly that.

We’re not creating a super fancy app here, just a simple app that takes some data and passes it through to the next ViewController via the prepareForSegue method, straightforward right? lets get to it.

1. Lets start by creating a single view application in Xcode, make sure to select Swift as the language for the project.

Step 1 - Create single view application

2. Create a new class file called ‘secondViewController’ and link it up to a new view controller in the story board, I am not going to go into detail in this post as I am assuming a basic knowledge of Xcode.

– Note, I am not sure if this is a bug with the beta version templates but you may want to comment out this code in the secondViewController else you will have an error when you run your app!

init(nibName nibNameOrNil: String?, bundle nibBundleOrNil: NSBundle?) {
super.init(nibName: nibNameOrNil, bundle: nibBundleOrNil)
// Custom initialization

3. Next up, add a text field and a button to the main ViewController and a label to the secondViewController and link them to the classes in the usual fashion, I have called my text field ‘textField’ and the label in secondViewController ‘labelPassedData’ which is of course where the data will be displayed.

Screen Shot 2014-06-28 at 21.24.44

4. Now we need to set up the segue – to do this, head back over to the storyboard and whilst holding down the CTRL key click the button and drag over to the second ViewController, you should then see this appear.

iOS Swift segue popup

You need to select show.

5. Now you should see an arrow line joining up the 2 ViewControllers, click on this and in the right panel attributes inspector you will see the option for an identifier, lets call it segueTest

iOS Swift segue identifier

6. Awesome – now for some coding! – head over to ViewController.swift and add this code under the ‘didReceiveMemoryWarning’ function

override func prepareForSegue(segue: UIStoryboardSegue!, sender: AnyObject!) {
if (segue.identifier == "segueTest") {
var svc = segue!.destinationViewController as secondViewController;

svc.toPass = textField.text


iOS swift - Xcode

7. You should now have an error up, this is due to secondViewController not having a property declared called toPass so lets go and add this to our secondViewController

var toPass:String!

This just adds the variable (or property) to the class and the exclamation mark just lets the class know that a value will be assigned to it – your error on ViewController.swift should now disappear.

8. In the secondViewController under the viewDidLoad function add this code

labelPassedData.text = toPass

9. Thats it! run the app, type some text in the field once the button is pressed, the next view should appear with your previously typed value.

I hope you enjoyed this tutorial and found it useful, this is my first Swift tutorial and I have many more planned so be sure to share on your favourite social networks and let me know in the comments below how you are getting on with Swift!

Menu Title