Stimulator 2.0.0

Stimulator 2.0.0

测试已测试
语言语言 SwiftSwift
许可证 MIT
释放最新版本2017年2月
SwiftSwift 版本3.0.2
SPM支持 SPM

ukitaka 维护。



  • 作者
  • yuki.takahashi

基于响应者链的定制事件处理。

使用方法

创建自定义事件和处理协议

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 子类(例如 UIViewUIViewController)中生成事件

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)
    }

}

要求

  • iOS 9.0+
  • Xcode8+

安装

Stimulator 通过 CocoaPods 提供。要安装它,只需将以下行添加到您的 Podfile 中

pod "Stimulator"

作者

yuki.takahashi, [email protected]

许可证

Stimulator 在 MIT 许可证下可用。有关更多信息,请参阅 LICENSE 文件。