Manage And Pass Data Between Controllers In Swift -Part 2

Arnold Lee
4 min readMay 25, 2018

--

OK~ Welcome to Part 2. If you haven’t read through Part 1, it’s never too late to start now! :)

And if you think these kind of tutorials are helpful to your project or your Swift learning or whatever you’re dealing with, please press that “clap button” for me. It’ll definitely give me more confident on keep posting stuff like these. Since I’m very new to this, never hesitate to point out mistakes I had made or asks questions you had in the comment section. OK~ Enough of the talking, let’s cut to the chase now.

As aways the final project will be post in the end :)

To Begin, we should be clear about what are the differences between Object, Class and Instance, for the reason that many beginners will be very confuse about these three terms, it took me a while to figure out the difference too. From “Object-Oriented Software Engineering” this book defines the three terms as below:

  • Object:An object is characterized by a number of operations and a state which remembers the effect of these operations. (p.44)
  • Class:A class represents a template for several objects and describes how these objects are structured internally. Objects of the same class have the same definition both for their operations and for their information structures. (p.50)
  • Instance:An instance is an object created from a class. The class describes the (behavior and information)structure of the instance, while the current state of the instance is defined by the operations performed on the instance. (p.50)

So we know that Class is not equal to Instance. I would describe Class as a mould and Instance as the product that was moulded. They sure do are relative but keep in mind they are not the same. Let’s continue where we left off.

3. Managing Previous Data

Here we’re going to use the concept of Instance, I accidentally discover this method while I was designing my app and to be honest I’m not sure if it’s the best way to deal with managing data, since there’s still methods like delegates or call back that are more complicate but still accomplish the same goal as mine.

Open Main.stroyboard and ViewController.swift, drag a new text field to ViewController1 and control drag to ViewController.swift and named it whatever you want.

Add the code below to viewDidLoad

myTextField.text = "Hello World"

Build it and run, you should now see Hello World in the text field as I do. Just for your notice we’re going to manage the text in myTextField while we’re on View Controller 2.

Next we’re going to use the two button above to gain access to View Controller 2, since this method works with both ways. Open ViewController2.swift, and create a variable named “instance” under the class. Be notice here we set the type as ViewController, which is the controller where we came from.

var instance: ViewController?

Go back to ViewController.swift, and look for the code below. The upper one is the one accessing to ViewController2 without segue and the other one is the one using segue.

Now add

sb.instance = self

to both of them. You might be curious what is self, self is the instance you current have in process, so by assigning to “sb.instance”, we’ll gain access of the instance of View Controller 1 while we’re at View Controller 2.

Your code should look like this now.

And we’re done with ViewController.swift close it and open ViewController2.swift and Main.storyboard. Create a new button and control drag an action to ViewController2.swift.

We’re almost there!! Add the code below to the button action we just created.

instance?.myTextField.text = "Happy Coding~ :)"
dismiss(animated: true, completion: nil)

The code in line 35 set the text of myTextField to the text we provide and line 36 simply dismiss View Controller 2 and take us back to View Controller 1 to see the result we just change.

Congratulation, we have accomplished managing data on previous controller, here I won’t be covering how to use delegates and call back because I seldom use them~ Happy coding :)

--

--

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.