Skip to content

Instantly share code, notes, and snippets.

@gracietti
Created April 5, 2017 04:10
Show Gist options
  • Save gracietti/b89520e4ce1e42d4584515c900dd9cf5 to your computer and use it in GitHub Desktop.
Save gracietti/b89520e4ce1e42d4584515c900dd9cf5 to your computer and use it in GitHub Desktop.
Sending messages with delegates
// 1. Declare which messages can be sent to the delegate
// File ProductCategoriesCoreDelegate.swift
protocol ProductCategoriesCoreDelegate: class {
//Add arguments if you need to send some information
func setupSubmodules(with product: Product)
}
// 2. Create a delegate to send him a message
// File ProductPresenter.swift
class ProductPresenter {
// MARK: Properties
weak var view: ProductView?
var router: ProductWireframe?
var interactor: ProductUseCase?
var categoriesDelegate: ProductCategoriesCoreDelegate?
var product: Product?
}
extension ProductPresenter: ProductPresentation {
func onViewDidLoad() {
if let product = product {
// Presenter initializes the delegate to whom he wants to send message
categoriesDelegate = router?.setupModule(with: product) // Make sure you customize this static function will return the delegate
// Presenter sends a message
categoriesDelegate?.setupSubmodules(with: product)
}
}
}
// 3. Implement the delegate protocol to do something when you receive the message
// ProductCategoriesPresenter.swift
class ProductCategoriesPresenter: ProductCategoriesPresentation {
...
}
extension ProductCategoriesPresenter: ProductCategoriesCoreDelegate {
func setupSubmodules(with product: Product) {
//Do what you nee here
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment