Delegate pattern

suggest change

A delegate is a common design pattern in the Cocoa and Cocoa Touch frameworks, where one class delegates some responsibility to another. The framework class implements generic behavior, and a separate delegate implements the specific use case.

Objects often need to talk to each other. One object conforms to a protocol so it can be another object’s delegate. The other object then calls back when something interesting happens.

For example, a view that displays a list should decide how the data is shown, not which data to show.

If you have two classes, a parent and a child:

class Parent { }
class Child { }

And you want to notify the parent of a change from the child.

In Swift, delegates are implemented using a protocol declaration and so we will declare a protocol which the delegate will implement. Here delegate is the parent object.

protocol ChildDelegate: class {
    func childDidSomething()
}

The child needs to declare a property to store the reference to the delegate:

class Child {
    weak var delegate: ChildDelegate?
}

delegate is optional, and ChildDelegate is class-only. Without that constraint the property cannot be weak, so the child could keep the parent alive and form a retain cycle. weak means the parent is released when nothing else references it. In Swift 4 and later, write AnyObject instead of class.

In this example, when the child does something and needs to notify its parent, the child will call:

delegate?.childDidSomething()

If the delegate has been defined, the delegate will be notified that the child has done something.

The parent class will need to extend the ChildDelegate protocol to be able to respond to its actions. This can be done directly on the parent class:

class Parent: ChildDelegate {
    ...

    func childDidSomething() {
        print("Yay!")
    }
}

Or using an extension:

extension Parent: ChildDelegate {
    func childDidSomething() {
        print("Yay!")
    }
}

The parent also needs to tell the child that it is the child’s delegate:

// In the parent
let child = Child()
child.delegate = self

By default a Swift protocol cannot declare optional requirements. Those exist only on an @objc protocol, using the optional modifier. The sample below is the Swift 2 form of that API.

UITableView implements generic table behavior. The app supplies UITableViewDelegate and UITableViewDataSource, which are protocols, not classes. They define how specific cells look and behave.

@objc public protocol UITableViewDelegate : NSObjectProtocol, UIScrollViewDelegate {
        
        // Display customization
        optional public func tableView(tableView: UITableView, willDisplayCell cell: UITableViewCell, forRowAtIndexPath indexPath: NSIndexPath)
        optional public func tableView(tableView: UITableView, willDisplayHeaderView view: UIView, forSection section: Int)
        optional public func tableView(tableView: UITableView, willDisplayFooterView view: UIView, forSection section: Int)
        optional public func tableView(tableView: UITableView, didEndDisplayingCell cell: UITableViewCell, forRowAtIndexPath indexPath: NSIndexPath)
        ...
}

You can implement this protocol by changing your class definition, for example:

class MyViewController : UIViewController, UITableViewDelegate

Any methods not marked optional in the protocol definition (UITableViewDelegate in this case) must be implemented.

Feedback about page:

Feedback:
Optional: your email if you want me to get back to you:



Table Of Contents