EventBusSwift
这个想法来自于 Android 中的 https://github.com/greenrobot/EventBus。
要求
- iOS 8.0+ / Mac OS X 10.10+ / watchOS 2.0+ / tvOS 9.0+
- Swift 3
- Xcode 8.0+
安装
CocoaPods
要使用 CocoaPods 安装 EventBusSwift,请将以下行添加到您的 Podfile
。
Swift 3.0.x
source 'https://github.com/CocoaPods/Specs.git'
platform :ios, '8.0' # or platform :osx, '10.10' if your target is OS X.
use_frameworks!
pod 'EventBusSwift'
然后运行 pod install
命令。有关 CocoaPods 的安装和使用详情,请访问 其官方网站。
文档
-
定义通知名称
extension Notification.Name { public static let Message = Notification.Name("Message") }
-
注册和注销您的订阅者。例如,在 iOS 中,UIViewController 通常应根据其生命周期进行注册
func onViewWillAppear(_ animated: Bool) { super.onViewWillAppear(animated) EvenBus.shared.register(self, name: .Message) { [weak self] (object) in guard let _ = self else { return } if let text = object as? String { print(text) } } } func onViewWillDisappear(_ animated: Bool) { super.onViewWillDisappear(animated) EvenBus.shared.unregister(self, name: .Message) }
-
发布事件
EvenBus.shared.post(name: .Message, object: "Hello EventBus")