Communicating using delegation
Delegation is a simple pattern that allows objects to communicate with each other in a loosely coupled interface. Delegation is also based on a protocol that allows classes to communicate with different types of objects.
Let’s see a small example of delegation:
protocol MyViewDelegate: AnyObject { func didTapButton() } class MyView: UIView { weak var delegate: MyViewDelegate? private let button = UIButton(type: .system) override init(frame: CGRect) { super.init(frame: frame) button.addTarget(self, action: #selector (buttonTapped), for: .touchUpInside) addSubview(button) } override func...