Back to Blog

Using Delegates for Object Passing Between ViewControllers

Posted by Flatiron School on November 5, 2013

The following is a guest post by James Lin and originally appeared on his blog. James is currently in the iOS-000 class at The Flatiron School. You can follow him on Twitter here.

Github link

Passing objects between views controllers is tricky in iOS.

In this example, I created a OneViewController and a TwoViewController. We are going to call them ONE and TWO for short.

We are going to pass a Person object from ONE to TWO.

Blog post image: tumblr_inline_mvspxgf8DI1rtan47.png

The Person class just has a ‘name’ property:

Blog post image: tumblr_inline_mvspy0wqlQ1rtan47.png

Lets say we create a Person class in ONE and stored it in a property called myPerson. How do we pass it to TWO? We are going to assign a property called otherPerson in TWO to point to ONE’s myPerson.

But how do we get access to a pointer to TWO? Since we are using a Navigation Controller, we can access it in the prepareForSegue method.

Blog post image: tumblr_inline_mvspyru31G1rtan47.png

Now lets say I want to create a Person object assigned to TWO’s myPerson property. How would I pass it back to ONE?

The bad way is to find a pointer to ONE and set its otherPerson property to TWO’s myPerson:

Blog post image: tumblr_inline_mvspzdcE0j1rtan47.png

Although this works, theres a couple of problems. First, it accesses the navigationControllers array to get to ONE. This is clearly pretty hacky. It also forces TWO to have to know about ONE, and can only return the Person it created to ONE. This is the tight coupling situation which we want to avoid. Second, suppose in the future if we wanted the button on TWO to be able to send the Person object to a new view controller called THREE instead? How would it do that?

The recommended solution is to use Delegates to pass the Person object.

We will first need to define a protocol on TWO

Blog post image: tumblr_inline_mvspzy0A0Y1rtan47.png

Now in ONE’s prepareForSegue method we need to set TWO’s delegate to self (the part I always forget.)

Blog post image: tumblr_inline_mvsq0iWxKQ1rtan47.png

We can erase any reference to ONE from TWO, such as an import that we needed before. The beauty of delegation is that TWO doesn’t have to know who implements it.

Blog post image: tumblr_inline_mvsq111ZvO1rtan47.png

We can change the sendButton action to pass the Person object back through the delegate:

Blog post image: tumblr_inline_mvsq1lIlKy1rtan47.png

Lastly, we implement the delegate method in ONE

Blog post image: tumblr_inline_mvsq2g00aP1rtan47.png

tldr:

Using the Delegate pattern for object back-passing in ViewControllers results in a cleaner and more flexible code. This is also the Apple recommended way.

About Flatiron School

More articles by Flatiron School