Manage And Pass Data Between Controllers In Swift -Part 1

Arnold Lee
4 min readMay 15, 2018

--

Passing data through controllers might be a dilemma to those newly joined programmers, but after reading this article, you’re going to learn the two most common ways to pass data and a way to pass your data back to your last view controller.

Initial settings

  1. Create a brand new project, name it whatever you want
  2. Drag a view controller from the Object Library

3. Drag a button from the Object Library and place it on the view controller that Xcode automatically generate for us.

4. Drag a textField from the Object Library and place it on the view controller we create.

5. Create a new file (File →New →File… →Cocoa Touch Class →Next →choose UIViewController in Subclass of: → name it whatever you like →Next →Create) for your second view controller. I’ll just simply named it ViewController2 here.

6. Assign “ViewController2.swift” to the view controller we just created

Now you should have this on your screen and we’re all set.

I added two labels to make it easier to distinguish the two controllers :)

1. Passing without segue

Remember the Identity inspector where we assign the new file to the view controller we create? Set StoryBoard ID in the inspector section, I’m going to set viewcontroller2 for convenience.

Control drag the button to viewContoller1, make sure to choose Action instead of Outlet here, named it passData.

Control drag the textField on View Controller 2 to viewController2, we’re going to use Outlet this time, named it myTextField and create a Variable called passText by the code below.

var passText: String?

Add the code below to viewDidLoad

myTextField.text = passText

From here we don’t need Storyboard anymore, let’s open up ViewController.swift and enter the code above in the IBAction we create earlier.

let sb = storyboard?.instantiateViewController(withIdentifier: “viewcontroller2”) as! ViewController2sb.passText = “Hello World”present(sb, animated: true, completion: nil)

Here we initial a constant “sb” and create an instance of ViewController2. Next we call the textField in sb and set it’s text as “Hello World” and we present sb. Your code should look like the code below.(Present should always be in the last line of function)

Congratulations, we have already finished passing the string “Hello World” to ViewController2 with just three lines of codes. Try to build it , you’ll see it. Pretty easy huh :)

2. Passing with Segue

Drag a button to View Controller 1 and drag a textField to View Controller 2. Connect the textField to class viewController2 like we did with the first textField and name it “myTextFieldSegue” and set a new variable “passTextSegue”

var passTextSegue: String?

Add the code below to viewDidLoad

myTextFieldSegue.text = passTextSegue
Your code should look like this now.

Control drag from the button we just created to View Controller 2(not the textField) choose “show”, now there should be a line between your 2 View Controller.

In ViewController.swift implement the code below

override func prepare(for segue: UIStoryboardSegue, sender: Any?) {let sb = segue.destination as! ViewController2sb.passTextSegue = "Hello World with segue"}

This code will run before executing the segue. You might be wondering why we don’t need to present sb because we already did it by connecting two view controllers.

Try to build and run it, it should work fine. If not don’t worry just check the project I post in the end of this article. :) Happy coding.

Part 2 will mainly be talking about managing the previous view controller’s datas, for example: I present View Controller 2 from View Controller 1 and I want to change View Controller 1’s button attributes while I was on View Controller 2. Feel free to ask me anything, I’ll answer you asap :))))

--

--

Arnold Lee

Hi~ I'm still very new at writing articles. Please never be hesitate to point out my mistakes or leave some feedbacks. I'll try my best to upload every month.