Skip to content

Latest commit

 

History

History
207 lines (131 loc) · 6.11 KB

File metadata and controls

207 lines (131 loc) · 6.11 KB

Protocols and Delegates

Why you should know this

The delegate pattern is widely used in iOS development to enable communication between objects.

🧐 We've seen it before in UITableViews and UICollectionViews.

If we understand how the delegate pattern works, we can implement our own to have control over how our classes communicate.

Learning Objectives

  1. Construct and use protocols to define 'contracts' in code
  2. Identify delegation in UIKit
  3. Implement delegates in code

Delegates

Meaning in the real world.

To delegate as a verb means to give control.
A delegate as a noun means a person acting on behalf of another.

In software:

Delegation is a design pattern that enables a class or structure to hand off (or delegate) some of its responsibilities to an instance of another type.

In iOS development, delegation is used as a way for one class to communicate to another class. It's a communication pattern.

The example that looks the most familiar to us is when using a tableview. The tableview by itself doesn’t know what information to show or the number of rows it needs to display. But it can find out by asking the dataSource.

A tableview’s dataSource must conform to the UITableViewDatasouce protocol, which has the right information needed to know what and how to display information.

The Boss and the Intern 👩🏻‍💻

We'll represent the communication pattern using the following analogy.

Boss - Knows the bugs that need to be fixed, chooses what to work on next.

Intern - Doesn't know anything yet and is just waiting for instructions from the boss.

The Boss will choose a ticket to fix a bug and assign it to the Intern. Once they receive instructions, the Intern can act according to the task.

Download the supporting files for the example.

BossVC

  • Step 1: Set up a protocol, this represents commands given to the Intern.
protocol TaskSelectionProtocol{
    func didSelectTask(task:String)
}

Here we have a list of commands for the Intern, could be one item or more.

  • Step 2: Create a variable to hold the delegate.
var selectionDelegate: TaskSelectionProtocol!
  • Step 3: Tell the delegate(intern) what we want to do.
selectionDelegate.didSelectTask(task: "New Feature")
selectionDelegate.didSelectTask(task: "Bug Fix")

InternVC

  • Step 4: Assign the delegate
bossVC.selectionDelegate = self

Here the InternVC is telling the BossVC instance that it wants to be its intern, by assigning itself as the selectionDelegate.

  • Step 5: Conform to the protocol - What to do after the Boss gives instructions
extension InternVC: TaskSelectionProtocol{
    func didSelectTask(task: String) {
        self.selectionLabel.text = "Working on \(task)"
    }
}

The Intern can proceed to work on the assigned task.

Can we say it in a diagram?

Using the example of boss/intern or other if it's easier for you, create a diagram to remember how delegates work.
Try to include the following key words: delegate, delegator and delegate protocol.

Add your own entry to this Jamboard.

Creating our own delegate

Download the starter files

Scenario: We have an app with two view controllers. FirstViewController has a button that takes us to SecondViewController. There we have 3 views of different colors. The goal is that by selecting one of the views, the app will take us back to the first view controller and change the background color to the one selected previously.

Implement a solution using delegates.

Completion handlers vs Delegation

Individually

Change the implementation of the boss/intern to use a completion handler.
If you are done early, practice doing the same for the 3 colors app.

Additional Resources

  1. Example for delegates
  2. Closures as delegates - article
  3. Understanding delegates - article
  4. Delegates simple analogy - article
  5. More on Delegates
  6. Intern/Boss analogy