Delegate

Introduction

Welcome to one of the most confusing topics of all time. You might have heard about the delegate pattern. Perhaps, you’ve seen, tableView.delegate = self. I’ve discovered a lot of developers have no idea how to explain what goes underneath. They just simply copy and paste as if it just works. In fact, I was one of them for solid 6 months when I first started learning iOS. Today, however, you are with me, Bob the Developer.

Do not worry about its vague terminology, called “delegate”. This is a pattern that is used to communicate or send data between objects that are created with classes and structs. That’s it. With that in mind, let me explain this magic for you.

Problem

How does delegate even work?

Purpose of Delegate: Communicate/Pass Data between objects

In this tutorial, you will learn how to send data from FirstVC to SecondVC using the delegate pattern.

Design Protocol

Create a protocol called, PassDataDelegate. It contains a method that takes data whose type is in String.

  1. protocol PassDataDelegate {
  2. func passData(data: String)
  3. }

Design Delegator (Sender)

Create a class that contains an optional property whose type is PassDataDelegate?.

  1. class FirstVC {
  2. var delegate: PassDataDelegate?
  3. }

If you call the method, passData of the property, delegate, it will return nil since you have not initialized delegate.

  1. FirstVC().delegate?.passData(data: "Bob") // nil

Design Delegate (Receiver)

Create SecondVC that conforms to PassDataDelegate. SecondVC must contain passData(data: String) due to the protocol.

  1. class SecondVC: PassDataDelegate {
  2. func passData(data: String) {
  3. print("The CEO gave me \(data)")
  4. }
  5. }

Create instances

  1. let firstVC = FirstVC()
  2. let secondVC = SecondVC()

Set firstVC.delegate to secondVC. It is possible since secondVC conforms to PassDataDelegate because Swift Protocol can be used as one’s type.

  1. firstVC.delegate = secondVC

When you set the relationship as above, you may call the passData method that resides in the secondVC object from the firstVCobject.

Assign Delegate

  1. firstVC.delegate = secondVC
  2. firstVC.delegate?.passData(data: "A bunch of contracts")
  3. // "The CEO gave me a bunch of contracts"

In the example above, firstVC is calling the passData method and it passes data, called, “A bunch of contracts”. When it occurs, the full implementation of the method that resides in secondVC get called. Thus, the print function is also get executed.

UITableView in ViewController

  1. import UIKit
  2. class BobViewController: UIViewController, UITableViewDelegate {
  3. let tableView = UITableView()
  4. override func viewDidLoad() {
  5. super.viewDidLoad()
  6. tableView.delegate = self
  7. }
  8. func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
  9. print(tableView)
  10. // Hey BobViewController/Delegate, do something with the data I've given you
  11. // Download Image...
  12. // ...
  13. }
  14. }

In the example above, tableView is the delegator/sender/CEO, while the object of BobViewController is the delegate/receiver/Secretary. The method, func tableView(_talbeView: UITableView, didSelectRowAt indexPath: IndexPath), is called by the tableView. Yet, the full implementation is executed within the BobViewController object.

Source code

4004_delegate.playground

Resources

The Complete Understanding of Delegate and DataSource

Introduction to Delegate in Swift

Conclusion

You’ve learned the delegate pattern is used to pass data between objects. In fact, you’ve learned how to pass data fromFirstVC to SecondVC in one direction. Simply put, you’ve learned passing data from the CEO to the secretary. In the following lesson, you will learn that you can also send data backward, the secretary to the CEO using data source. You will find out.

If you have not grasped the concept of delegate, I recommend you to ask questions on Udemy, Slack, or any other platforms. I’ve also attached additional articles for you to take a look at. You’ve got to know the principle because in the next course, you will learn how this pattern is to communicate between iOS, the operating system, and us, developers.