iOS Swift Tutorial – passing data between ViewControllers

Since Apples spectacular WWDC announcement of a new programming language Swift, I have been super excited to learn this new language, so like many, I grabbed the beta version of Xcode 6 and started coding!

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 6, 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!

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

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

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

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!

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s