Tutorial – Pass data between View Controllers using a Segue
In the last tutorial we looked at navigating view controllers in swift, in this one we are going to look at how to pass data when navigating to another view controller.
To begin with create a new XCode single view application project, and layout the storyboard as follows:
- Create a navigation controller, linked to the default view controller as the root view controller.
- On the default view controller place two buttons, red and blue.
- Link these buttons by a segue to a new view controller (Control click on the button and drag and drop the line to the new view controller, select the show segue)
Now we need a new ViewController class for our blank view controller at the right side. Go FileàNewàFile and create a new iOS Source, Cocoa touch class. Name the class ColorView, with the subclass of being UIViewController.
Now go back to your Storyboard, and select the blank view controller we added in on the right side. From the Identity Inspector change the class to ColorView.
Now we have our app setup – each button will navigate to the ColorView using a segue. We need a way to differentiate between what button was pressed as we will use this to set the background colour of ColorView.
To do this in the storyboard we click on the segue lines . Click on the top one which should be for the Blue button. In the attributes inspector name the segue sgBlue.
Do the same process for the other segue for the Red button, naming it sgRed. Now that this is setup we need to add a variable in ColorView.swift that will store the passed data. Up the top above view did load add in the following string variable.
class ColorView: UIViewController { var passedColor = String() override func viewDidLoad() {
Note: The variable type can be anything you want, a Integer, a UIColor and so on. In this example we are just using the String datatype.
Time to do the code to pass the variable! Go back to our ViewController.swift and add the following block of code in:
override func prepare(for segue: UIStoryboardSegue, sender: Any?) { let vc = segue.destination as! ColorView // Get our ColourView print(segue.identifier) // Show the segue identifier that is being run if segue.identifier == "sgRed" { vc.passedColor = "Red" // Pass the colour red to the passedColor varible in ColorView } else if segue.identifier == "sgBlue" { vc.passedColor = "Blue" // Pass the colour blue to the passedColor varible in ColorView } }
Once this is done go to ColorView.swift and add the following code in viewDidLoad():
override func viewDidLoad() { super.viewDidLoad() print(passedColor) if passedColor == "Red" { self.view.backgroundColor = UIColor.red } else if passedColor == "Blue" { self.view.backgroundColor = UIColor.blue } }
The code should be pretty straight forward, basically we get the segue identifier and based on it’s value we store either Blue or Red in the passedData varible in ColorView. From this we then change the background of the view to be either Red or Blue.
Run the app to see it in action! You can see the console messages to see exactly what is happening with the println() statements, also you will notice depending on what button is pressed the background color in the next view controller will change, neat!