测试已测试 | ✗ |
语言语言 | SwiftSwift |
许可证 | MIT |
释放最新版本 | 2017年2月 |
SwiftSwift 版本 | 3.0.2 |
SPM支持 SPM | ✓ |
由 ukitaka 维护。
基于响应者链的定制事件处理。
import Stimulator
struct ShowAlertEvent : Stimulator.Event {
typealias Responder = ShowAlertResponder
let title: String
let message: String
init(_ title: String, _ message: String) {
self.title = title
self.message = message
}
func stimulate(responder: Responder) {
responder.showAlert(event: self)
}
}
protocol ShowAlertResponder {
func showAlert(event: ShowAlertEvent)
}
在 UIResponer
子类(例如 UIView
、UIViewController
)中生成事件
self.stimulate(event: ShowAlertEvent("title", "message"))
class MyViewController: UIViewController, ShowAlertResponder {
func showAlert(event: ShowAlertEvent) {
let alert = UIAlertController(title: event.title, message: event.message, preferredStyle: UIAlertControllerStyle.alert)
alert.addAction(UIAlertAction(title: "OK", style: UIAlertActionStyle.cancel, handler: { _ in }))
self.show(alert, sender: nil)
}
}
您可以使用协议扩展来提供默认实现。
extension ShowAlertResponder where Self : UIViewController {
func showActionSheet(event: ShowActionSheetEvent) {
let alert = UIAlertController(title: event.title, message: event.message, preferredStyle: UIAlertControllerStyle.alert)
alert.addAction(UIAlertAction(title: "OK", style: UIAlertActionStyle.cancel, handler: { _ in }))
self.showViewController(alert, sender: nil)
}
}
Stimulator 通过 CocoaPods 提供。要安装它,只需将以下行添加到您的 Podfile 中
pod "Stimulator"
yuki.takahashi, [email protected]
Stimulator 在 MIT 许可证下可用。有关更多信息,请参阅 LICENSE 文件。