• Home
  • About
  • Our Apps
  • Learn
  • Contact
Login

Register

Login
Seemu Apps Seemu Apps
  • Home
  • About
  • Our Apps
  • Learn
  • Contact

Tutorial – Pass data between View Controllers using a Segue

Home ios Tutorial – Pass data between View Controllers using a Segue

Tutorial – Pass data between View Controllers using a Segue

Aug 2, 2015 | Posted by Andrew | ios, swift, tutorial, xcode |

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:

  1. Create a navigation controller, linked to the default view controller as the root view controller.
  2. On the default view controller place two buttons, red and blue.
  3. 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)

Screen Shot 2015-08-02 at 10.34.30 am

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.

Screen Shot 2015-08-02 at 10.36.32 am

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.

Screen Shot 2015-08-02 at 10.38.10 am

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 Screen Shot 2015-08-02 at 10.39.11 am. Click on the top one which should be for the Blue button. In the attributes inspector name the segue sgBlue.

Screen Shot 2015-08-02 at 10.40.25 am

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!

DownloadSourceCode

Tags: datanavigationnavigation controllerpasssegueview controller
1
Share

About Andrew

Andrew is a 24 year old from Sydney. He loves developing iOS apps and has done so for two years.

You also might be interested in

How to save data, settings & preferences with NSUserDefaults

Jan 5, 2016

One of the very first questions you have after learning[...]

Tutorial – Transparent UI Navigation Bar

Feb 21, 2016

Making the UINavigation bar transparent & see through is easy[...]

Swift – View Controller Lifecycle

May 24, 2017

As you develop iOS apps you will use more and[...]

Welcome

Hi I am Andrew and welcome to Seemu Apps! Have a look around, we provide tutorials for primarily iOS apps.
Bluehost website hosting discount

Seemu’s Studio Setup

Blue Yeti Microphone
Rode Stand
Spider Shock Mount
Mac Keyboard Cover
Screenflow - recording software

Contact Us

We're currently offline. Send us an email and we'll get back to you, asap.

Send Message

Footer

:)

© 2025 · Your Website. Theme by HB-Themes.

  • Home
  • About
  • Our Apps
  • Learn
  • Contact
Prev Next