Multicast delegate framework written on Swift 4.2.
Features | |
---|---|
🎭 | Adding multicast delegate to your custom classes |
🚴 | Subclasses of UIKit controls with multicast delegate property (currently only for UITableView) |
🚀 | Manage which delegates responsible for returning value for UIKit controls using responsibleForSelectors() function |
To run the example project, clone the repo, and run pod install
from the Example directory first.
let tableView = MultidelegateTableView()
tableView.multiDelegate.add(delegate: self)
protocol FooClassDelegate {
func fooEvent()
}
class FooClass {
var multicastDelegate = MulticastDelegate<FooClassDelegate>()
func foo() {
multicastDelegate.invoke { delegate in
delegate.fooEvent()
}
}
}
class BarClass: FooClassDelegate {
var foo = FooClass()
var baz = BazClass()
init() {
foo.multicastDelegate.add(delegate: self)
foo.multicastDelegate.add(delegate: baz)
}
func fooEvent() {
}
}
class BazClass: FooClassDelegate {
func fooEvent() {
}
}
Some delegate methods can return a value. So when we have several delegates, we need a mechanism to manage which delegate is responsible for returning value. Only one delegate should be responsible for returning value, other delegates are listeners.
class FooClass: UITableViewDelegate, MulticastableDelegate {
var tableView: MultidelegateTableView
func setup() {
tableView.multiDelegate.add(delegate: self)
}
func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
return 100
}
func responsibleForSelectors() -> [String]? {
return [#selector(tableView(_:heightForRowAt:)).description]
}
}
iOS 10.0+ Swift 4.0+
MulticastDelegate is available through CocoaPods. To install it, simply add the following line to your Podfile:
pod 'MulticastDelegateKit'
Alex Kisel, alexander.kisel@brander.ua
MulticastDelegate is available under the MIT license. See the LICENSE file for more info.