Redundant Delegate Pattern

Delegate is commonly used in building iOS / OSX apps. Common ones include those in building UITableView or UICollecionView. At the very least, we shall provide the content through a data source protocol, which is based on the delegate pattern.

While it is a common pattern in building Cocoa / Cocoa Touch applications, delegate is sometimes quite annoying, especially you are building several levels.

Multi-level Delegates

UICollectionView -> UICollectionViewCell

I am building a simple todo list application, that has a structure like this.

ViewController -> UICollectionView -> UICollectionViewCell -> UITextField

This structure is quite general. The outside ViewController contains the collection view, and is responsible to store an array of todo items, and serve as data source.

For instance, I want to get notified when the UITextField begins and ends editing. I wan need to setup the delegate inside-out.

Step 1: Set the UITextFieldDelegate to UICollectionViewCell

Step 2: Set a custom UICollectionViewCellDelegate to UICollectionView

Step 3: Set a custom UICollectionViewDelegate to ViewController (optional)

The 3rd step is optional, depending on how you reference to collection view. Say I could skip it by placing collectionView within ViewController, so that I can set UICollectinoViewCellDelegate to ViewController directly in the data source protocol method.

This whole process is sometimes quite annoying, as UICollectionViewCell actually does not handle the delegate call, it is literally serving as a bridge and passing the call up the hierarchy.


Like what you read? Give Harry Ng a round of applause.

From a quick cheer to a standing ovation, clap to show how much you enjoyed this story.