EventBus
EventBus 是一个用于组件之间通信的小型、简单的类型化事件总线,它基于 NSNotificationCenter 构建。
如何安装
pod 'BKEventBus'
为什么使用 EventBus?
如果您想删除组件或层之间的依賴关系,最好使用事件总线。在您构建软件时,删除依賴关系总是件重要的事。EventBus 帮助您更轻松地做到这一点。它是一个类型化的事件总线。您可以为 Event 类型定义事件和参数,并在需要的地方使用它们。
如何使用 EventBus
定义 EventType
您可以使用EventType协议来定义事件类型。这只是在标记事件类型。您可以使用所有类型,如类、结构体或枚举。我喜欢使用枚举 :)。您可以按以下方式定义事件
enum Actions: EventType {
case push(item: String)
case pop
case loading(item: String)
case delete(item: String)
}
创建活动海报
...
let eventBus: EventBus<Actions> = EventBus()
...
发布活动
eventBus.post(event: .push(item: "Something"))
eventBus.post(event: .pop)
eventBus.post(event: .loding(item: "1234"))
eventBus.pose(event: .delete(item: "1234"))
订阅事件
// Somewhere
{
...
let eventBus: EventBus<Actions> = EventBus()
eventBus.on { [weak self] (event: Actions) in
switch event {
case let .push(item):
...
case pop:
...
case loding(item):
...
case delete(item):
...
}
}
...
}
生命周期
您可以使用生命周期方法依次停止或恢复事件总线。
恢复事件总线
eventBus.resume()
停止事件总线
eventBus.stop()
或者您可以通过删除它来停止事件总线。
// Define Event Bus
var eventBus: EventBus<Actions>? = EventBus()
...
Use it
...
// Delete it
eventBus = nil
简单示例
CollectionViewCell与ViewController之间的通讯。
在Cell中定义事件
class ItemCollectionViewCell: UICollectionViewCell {
// Define Events
enum Event: EventType {
case clickedDeleteButton(indexPath: IndexPath)
case clickedDetailButton(indexPath: IndexPath)
}
static var className: String {
return String(describing: self)
}
@IBOutlet weak var dateLabel: UILabel!
private var indexPath: IndexPath? = nil
private var eventBus: EventBus<ItemCollectionViewCell.Event>? = nil
override func awakeFromNib() {
super.awakeFromNib()
}
override func prepareForReuse() {
super.prepareForReuse()
eventBus = nil
}
func configure(item: String, indexPath: IndexPath) {
self.dateLabel.text = item
self.indexPath = indexPath
eventBus = EventBus()
}
// Post Events
@IBAction func clickedDetailButton(_ sender: Any) {
guard let indexPath = indexPath else { return }
eventBus?.post(event: .clickedDetailButton(indexPath: indexPath))
}
@IBAction func clickedDeleteButton(_ sender: Any) {
guard let indexPath = indexPath else { return }
eventBus?.post(event: .clickedDeleteButton(indexPath: indexPath))
}
}
在ViewController上订阅事件
import UIKit
class ListViewController: UIViewController {
@IBOutlet weak var collectionView: UICollectionView!
private var items: [String] = []
private let eventBus = EventBus<ItemCollectionViewCell.Event>()
override func viewDidLoad() {
super.viewDidLoad()
self.title = "Date List"
collectionView.register(UINib(nibName: ItemCollectionViewCell.className, bundle: nil), forCellWithReuseIdentifier: ItemCollectionViewCell.className)
collectionView.dataSource = self
collectionView.delegate = self
// Subscribe events
eventBus.on { [weak self] (event: ItemCollectionViewCell.Event) in
guard let strongSelf = self else { return }
switch event {
case let .clickedDeleteButton(indexPath):
strongSelf.items.remove(at: indexPath.row)
strongSelf.collectionView.reloadData()
case let .clickedDetailButton(indexPath):
let item = strongSelf.items[indexPath.row]
let vc = DetailViewController.viewController(item: item)
strongSelf.navigationController?.pushViewController(vc, animated: true)
}
}
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
}
@IBAction func clickedAddButtonItem(_ sender: Any) {
let date = Date()
items.append(date.description)
collectionView.reloadData()
}
}
extension ListViewController: UICollectionViewDataSource, UICollectionViewDelegateFlowLayout {
func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
return items.count
}
func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize {
return CGSize(width: collectionView.bounds.width, height: 50)
}
func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
let cell = collectionView.dequeueReusableCell(withReuseIdentifier: ItemCollectionViewCell.className, for: indexPath)
if let itemCell = cell as? ItemCollectionViewCell {
itemCell.configure(item: items[indexPath.row], indexPath: indexPath)
}
return cell
}
}
MIT许可
MIT许可
版权所有 © 2018 Sungcheol Kim, http://github.com/ReactComponentKit/BKEventBus
在此特此授予任何获得本软件及其相关文档副本(以下简称“软件”)的个人在此软件中无限制处理的权利,包括但不限于使用、复制、修改、合并、发布、分发、再许可和/或出售软件副本,以及允许向获得软件副本的个人提供上述行为,前提是遵守以下条件
上述版权声明和本许可声明应包含在本软件的所有副本或实质性部分中。
本软件按“原样”提供,不提供任何形式的保证,无论是明确保证还是暗示保证,包括但不限于适销性、特定用途适用性和非侵权性保证。在任何情况下,作者或版权持有人不对任何索赔、损害或其他责任承担责任,无论是由合同、侵权或其他行为引起的,无论该索赔、损害或其他责任是否与本软件或本软件的使用或其他交易有关。